mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
implement file upload via plugin
This commit is contained in:
79
plugin/main.js
Normal file
79
plugin/main.js
Normal file
@@ -0,0 +1,79 @@
|
||||
const { Plugin, Notice, TFolder } = require("obsidian");
|
||||
|
||||
class IgnisBridgePlugin extends Plugin {
|
||||
async onload() {
|
||||
console.log("[ignis-bridge] Plugin loaded");
|
||||
|
||||
this.addRibbonIcon("upload", "Upload file", () => {
|
||||
this.showFilePicker();
|
||||
});
|
||||
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("file-menu", (menu, file) => {
|
||||
if (file instanceof TFolder) {
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle("Upload file")
|
||||
.setIcon("upload")
|
||||
.onClick(() => {
|
||||
this.showFilePicker(file);
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
showFilePicker(targetFolder = null) {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.multiple = true;
|
||||
input.style.display = "none";
|
||||
|
||||
input.addEventListener("change", async () => {
|
||||
const files = Array.from(input.files || []);
|
||||
if (files.length === 0) return;
|
||||
|
||||
const folder = targetFolder || this.app.vault.getRoot();
|
||||
const folderPath = folder.path;
|
||||
|
||||
new Notice(`Uploading ${files.length} file(s)...`);
|
||||
|
||||
let successCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const targetPath = folderPath
|
||||
? `${folderPath}/${file.name}`
|
||||
: file.name;
|
||||
|
||||
await this.app.vault.createBinary(targetPath, arrayBuffer);
|
||||
successCount++;
|
||||
} catch (e) {
|
||||
console.error("[ignis-bridge] Upload failed:", file.name, e);
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (successCount > 0) {
|
||||
new Notice(`Uploaded ${successCount} file(s) successfully`);
|
||||
}
|
||||
if (errorCount > 0) {
|
||||
new Notice(`Failed to upload ${errorCount} file(s)`, 5000);
|
||||
}
|
||||
|
||||
input.remove();
|
||||
});
|
||||
|
||||
document.body.appendChild(input);
|
||||
input.click();
|
||||
}
|
||||
|
||||
onunload() {
|
||||
console.log("[ignis-bridge] Plugin unloaded");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = IgnisBridgePlugin;
|
||||
10
plugin/manifest.json
Normal file
10
plugin/manifest.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "ignis-bridge",
|
||||
"name": "Ignis Bridge",
|
||||
"version": "1.0.0",
|
||||
"minAppVersion": "1.0.0",
|
||||
"description": "Upload files from your device to the vault",
|
||||
"author": "Ignis",
|
||||
"authorUrl": "https://github.com/Nystik-gh/ignis",
|
||||
"isDesktopOnly": false
|
||||
}
|
||||
Reference in New Issue
Block a user