From a00274d5c9a381e4e6e5ec1e2af9354559379d74 Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:33:32 -0400 Subject: [PATCH 1/7] fix subfoldering --- src/commands/utils.ts | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/commands/utils.ts b/src/commands/utils.ts index 2166d96..530017f 100644 --- a/src/commands/utils.ts +++ b/src/commands/utils.ts @@ -164,26 +164,35 @@ export function isType(url: string, formats: string[]): boolean { // Determine the subfolder to place the uploaded // file in, based on file type uploaded export function setSubfolder(file: File, resourceUrl: string, plugin: CloudinaryUploader): string { + let result=""; if (file) { - if (file.type && file.type.startsWith("image")) { - return `${plugin.settings.folder}/${plugin.settings.imageSubfolder}`; - } else if (file.type.startsWith("audio")) { - return `${plugin.settings.folder}/${plugin.settings.audioSubfolder}`; - } else if (file.type.startsWith("video")) { - return `${plugin.settings.folder}/${plugin.settings.videoSubfolder}`; - } else { - return `${plugin.settings.folder}/${plugin.settings.rawSubfolder}`; + if (file.type && file.type.startsWith("image") && plugin.settings.imageSubfolder) { + result = `${plugin.settings.imageSubfolder}`; + } else if (file.type.startsWith("audio") && plugin.settings.audioSubfolder) { + result = `${plugin.settings.audioSubfolder}`; + } else if (file.type.startsWith("video") && plugin.settings.videoSubfolder ) { + result = `${plugin.settings.videoSubfolder}`; + } else if(plugin.settings.rawSubfolder) { + result = `${plugin.settings.rawSubfolder}`; } } else if (resourceUrl) { - if (isType(resourceUrl, imageFormats)) { - return `${plugin.settings.folder}/${plugin.settings.imageSubfolder}`; - } else if (isType(resourceUrl, audioFormats)) { - return `${plugin.settings.folder}/${plugin.settings.audioSubfolder}`; - } else if (isType(resourceUrl, videoFormats)) { - return `${plugin.settings.folder}/${plugin.settings.videoSubfolder}`; - } else { - return `${plugin.settings.folder}/${plugin.settings.rawSubfolder}`; + if (isType(resourceUrl, imageFormats) && plugin.settings.imageSubfolder) { + result = `${plugin.settings.folder}/${plugin.settings.imageSubfolder}`; + } else if (isType(resourceUrl, audioFormats) && plugin.settings.audioSubfolder) { + result = `${plugin.settings.folder}/${plugin.settings.audioSubfolder}`; + } else if (isType(resourceUrl, videoFormats) && plugin.settings.videoSubfolder) { + result = `${plugin.settings.folder}/${plugin.settings.videoSubfolder}`; + } else if(plugin.settings.rawSubfolder) { + result = `${plugin.settings.folder}/${plugin.settings.rawSubfolder}`; } } + if(plugin.settings.folder && result != ""){ + result = `${plugin.settings.folder}/${result}` + }else if(plugin.settings.folder){ + result = `${plugin.settings.folder}`; + }else{ + result =""; + } + return result; } From 2d3db5330718cb860129a9a665483f67fc3a6202 Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:34:48 -0400 Subject: [PATCH 2/7] fix unused import --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index aa51848..9457053 100755 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { import axios from "axios" import objectPath from 'object-path' import { v2 as cloudinary } from 'cloudinary'; -import { uploadVault, uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl,generateTransformParams,fetchMessages } from "./commands/utils"; +import { uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl,generateTransformParams,fetchMessages } from "./commands/utils"; // Settings tab import From c12d1dd795884056acf1378d5d1414cacc5d2ef8 Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:05:20 -0400 Subject: [PATCH 3/7] fix messaging for mass actions --- src/commands/utils.ts | 13 +++++++--- src/main.ts | 57 ++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/commands/utils.ts b/src/commands/utils.ts index 530017f..54ca6df 100644 --- a/src/commands/utils.ts +++ b/src/commands/utils.ts @@ -85,7 +85,9 @@ export async function fetchMessages(plugin:CloudinaryUploader) : Promise{ } }); } -export function uploadCurrentNoteFiles(file: TFile, plugin: CloudinaryUploader): void { +export async function uploadCurrentNoteFiles(file: TFile, plugin: CloudinaryUploader): Promise { + let successMessages = []; + let failureMessages = []; //! Read a cached version of the file, then: /* * Find a RegEx match for [[]] file refs @@ -101,7 +103,7 @@ export function uploadCurrentNoteFiles(file: TFile, plugin: CloudinaryUploader): let filePath; const adapter = plugin.app.vault.adapter; if (adapter instanceof FileSystemAdapter) { - filePath = adapter.getFullPath(fileString) + filePath = adapter.getFullPath(fileString); cloudinary.uploader.unsigned_upload(filePath, plugin.settings.uploadPreset, { folder: setSubfolder(undefined, filePath, plugin), resource_type: 'auto' @@ -115,14 +117,17 @@ export function uploadCurrentNoteFiles(file: TFile, plugin: CloudinaryUploader): plugin.app.vault.process(file, () => { return data; }) - new Notice("Upload of note file was completed"); // Success + successMessages.push('success') + //new Notice("Upload of note file was completed"); // Success }, err => { + failureMessages.push('error uploading file: '+filePath+' '+err.message); // Failure - new Notice("There was something wrong with your upload. Please try again. " + file.name + '. ' + err.message, 0); + //new Notice("There was something wrong with your upload. Please try again. " + file.name + '. ' + err.message, 0); }) } } }); + return failureMessages; } // Called to generate the output of the transformation parameters // that are set on uploads diff --git a/src/main.ts b/src/main.ts index 9457053..5850e2a 100755 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { import axios from "axios" import objectPath from 'object-path' import { v2 as cloudinary } from 'cloudinary'; -import { uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl,generateTransformParams,fetchMessages } from "./commands/utils"; +import { uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl, generateTransformParams, fetchMessages } from "./commands/utils"; // Settings tab import @@ -25,9 +25,9 @@ export default class CloudinaryUploader extends Plugin { callback: () => { let file = this.app.workspace.getActiveFile(); if (this.settings.ignoreWarnings) { - uploadCurrentNoteFiles(file,this); + uploadCurrentNoteFiles(file, this); } else { - uploadNoteModal(file, 'note',this); + uploadNoteModal(file, 'note', this); } } @@ -36,15 +36,23 @@ export default class CloudinaryUploader extends Plugin { id: "upload-all-note-files-cloudinary", name: "Upload all note files to Cloudinary", callback: () => { - const files = this.app.vault.getMarkdownFiles() - if (this.settings.ignoreWarnings) { - for (let file of files) { - uploadCurrentNoteFiles(file,this); + this.uploadAllNotes().then((returns) => { + let errorFlag = false; + if (returns.length > 0) { + for (let msgs of returns) { + if (msgs.length > 0) { + errorFlag = true + new Notice("There were errors completing your operation. Please look at the developer console for further information", 0); + for (let msg of msg) { + console.warn(msg); + } + } + + } + } if (!errorFlag) { + new Notice("The operation is complete. No errors to report", 0); } - - } else { - uploadNoteModal(undefined, 'note',this); - } + }); } }); @@ -56,11 +64,26 @@ export default class CloudinaryUploader extends Plugin { //async fetch messages after upload of vault assets fetchMessages(this); } else { - uploadNoteModal(undefined, 'asset',this); + uploadNoteModal(undefined, 'asset', this); } } }); } + private async uploadAllNotes(): Promise { + let returnData = [] + const files = this.app.vault.getMarkdownFiles() + if (this.settings.ignoreWarnings) { + for (let file of files) { + await uploadCurrentNoteFiles(file, this).then((res) => { + returnData.push(res); + }); + } + return returnData; + + } else { + uploadNoteModal(undefined, 'note', this); + } + } private clearHandlers(): void { this.app.workspace.off('editor-paste', this.pasteHandler); this.app.workspace.off('editor-drop', this.dropHandler); @@ -113,7 +136,7 @@ export default class CloudinaryUploader extends Plugin { const formData = new FormData(); formData.append('file', file); formData.append('upload_preset', this.settings.uploadPreset); - formData.append('folder', this.settings.folder != '' ? setSubfolder(file, undefined,this) : ''); + formData.append('folder', this.settings.folder != '' ? setSubfolder(file, undefined, this) : ''); // Make API call axios({ @@ -126,7 +149,7 @@ export default class CloudinaryUploader extends Plugin { let url = objectPath.get(res.data, 'secure_url'); let resType = objectPath.get(res.data, 'resource_type'); // Split URL to allow for appending transformations - url = generateTransformParams(url,this); + url = generateTransformParams(url, this); let replaceMarkdownText = generateResourceUrl(file.type, url); // Show MD syntax using uploaded image URL, in Obsidian Editor this.replaceText(editor, pastePlaceText, replaceMarkdownText) @@ -162,21 +185,21 @@ export default class CloudinaryUploader extends Plugin { } } } - + // Plugin load steps async onload(): Promise { console.log("loading Cloudinary Uploader"); await this.loadSettings(); this.clearHandlers(); this.setupHandlers(); - this.addSettingTab(new CloudinaryUploaderSettingTab(this.app,this)); + this.addSettingTab(new CloudinaryUploaderSettingTab(this.app, this)); // Set cloudinary cloud name config for node module cloudinary.config({ cloud_name: this.settings.cloudName }); this.setCommands(); - } + } // Plugin shutdown steps onunload(): void { From 05cb8b1334431255c8d3b84bfebf93e8dccd4dd4 Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:17:31 -0400 Subject: [PATCH 4/7] refactoring for mass uploads --- src/main.ts | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5850e2a..5f3f27d 100755 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import { import axios from "axios" import objectPath from 'object-path' import { v2 as cloudinary } from 'cloudinary'; -import { uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl, generateTransformParams, fetchMessages } from "./commands/utils"; +import { uploadNoteModal, uploadCurrentNoteFiles, setSubfolder, generateResourceUrl, generateTransformParams, fetchMessages, uploadAllNotes } from "./commands/utils"; // Settings tab import @@ -36,14 +36,15 @@ export default class CloudinaryUploader extends Plugin { id: "upload-all-note-files-cloudinary", name: "Upload all note files to Cloudinary", callback: () => { - this.uploadAllNotes().then((returns) => { + if(this.settings.ignoreWarnings){ + uploadAllNotes(this).then((returns) => { let errorFlag = false; if (returns.length > 0) { for (let msgs of returns) { if (msgs.length > 0) { errorFlag = true new Notice("There were errors completing your operation. Please look at the developer console for further information", 0); - for (let msg of msg) { + for (let msg of msgs) { console.warn(msg); } } @@ -55,6 +56,7 @@ export default class CloudinaryUploader extends Plugin { }); } + } }); this.addCommand({ id: "upload-all-media-assets-cloudinary", @@ -69,21 +71,7 @@ export default class CloudinaryUploader extends Plugin { } }); } - private async uploadAllNotes(): Promise { - let returnData = [] - const files = this.app.vault.getMarkdownFiles() - if (this.settings.ignoreWarnings) { - for (let file of files) { - await uploadCurrentNoteFiles(file, this).then((res) => { - returnData.push(res); - }); - } - return returnData; - } else { - uploadNoteModal(undefined, 'note', this); - } - } private clearHandlers(): void { this.app.workspace.off('editor-paste', this.pasteHandler); this.app.workspace.off('editor-drop', this.dropHandler); From 0f7a47a087258a4e7f86cd1ae4a658a25e167c6f Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:31:27 -0400 Subject: [PATCH 5/7] max upload logic error --- src/commands/utils.ts | 38 ++++++++++++++++++++++++++++++++------ src/main.ts | 12 ++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/commands/utils.ts b/src/commands/utils.ts index 54ca6df..9067310 100644 --- a/src/commands/utils.ts +++ b/src/commands/utils.ts @@ -22,12 +22,26 @@ export function uploadNoteModal(file?: TFile, type: string, plugin: CloudinaryUp fetchMessages(plugin); //uploadVault(plugin); // Upload vault function return; - } else if (type == 'note') { //! If no file passed, but 'notes' to be uploaded, this means all notes are requested. - const files = plugin.app.vault.getMarkdownFiles() - for (let file of files) { - uploadCurrentNoteFiles(file, plugin); - } - + } else if (type == 'note') { //! If no file passed, but 'notes' to be uploaded, this means all notes are requested. + uploadAllNotes(plugin).then((returns) => { + let errorFlag = false; + if (returns.length > 0) { + for (let msgs of returns) { + if (msgs.length > 0) { + + // If errors + errorFlag = true + new Notice("There were errors completing your operation. Please look at the developer console for further information", 0); + for (let msg of msgs) { + console.warn(msg); + } + } + + } + } if (!errorFlag) { + new Notice("The operation is complete. No errors to report", 0); + } + }); } } } else { @@ -36,6 +50,18 @@ export function uploadNoteModal(file?: TFile, type: string, plugin: CloudinaryUp }).open(); } +// This function used to upload mass note files +export async function uploadAllNotes(plugin:CloudinaryUploader): Promise { + let returnData = [] + const files = plugin.app.vault.getMarkdownFiles() + for (let file of files) { + await uploadCurrentNoteFiles(file, plugin).then((res) => { + returnData.push(res); + }); + } + return returnData; +} + export async function uploadVault(plugin: CloudinaryUploader): Promise { let successMessages = []; diff --git a/src/main.ts b/src/main.ts index 5f3f27d..17e5f4c 100755 --- a/src/main.ts +++ b/src/main.ts @@ -33,15 +33,25 @@ export default class CloudinaryUploader extends Plugin { } }); this.addCommand({ + //* If we're uploading all note files, we loop over the standard + //* note file upload function id: "upload-all-note-files-cloudinary", name: "Upload all note files to Cloudinary", callback: () => { if(this.settings.ignoreWarnings){ + // If we were to do the standard loop, + // success / failure messages would be shown for each + // success / failure. Has the potential to hang up the program + // if there are many assets uploaded. + + // Upload Notes waits until all uploads done uploadAllNotes(this).then((returns) => { let errorFlag = false; if (returns.length > 0) { for (let msgs of returns) { if (msgs.length > 0) { + + // If errors errorFlag = true new Notice("There were errors completing your operation. Please look at the developer console for further information", 0); for (let msg of msgs) { @@ -55,6 +65,8 @@ export default class CloudinaryUploader extends Plugin { } }); + }else{ + uploadNoteModal(undefined,'note',this); } } }); From 19cdc01d992ca2a114775b375582e1f4a2195e4a Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:31:43 -0400 Subject: [PATCH 6/7] bump version --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index b321456..4852296 100755 --- a/manifest.json +++ b/manifest.json @@ -5,5 +5,5 @@ "author": "Jordan Handy", "isDesktopOnly": true, "minAppVersion": "0.11.0", - "version": "1.0.0" + "version": "1.0.1" } From c92436d8f30e4b32b74fdd16daadc0e132d13ae6 Mon Sep 17 00:00:00 2001 From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:39:31 -0400 Subject: [PATCH 7/7] fix foldering logic --- src/commands/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/utils.ts b/src/commands/utils.ts index 9067310..699e09d 100644 --- a/src/commands/utils.ts +++ b/src/commands/utils.ts @@ -219,6 +219,8 @@ export function setSubfolder(file: File, resourceUrl: string, plugin: Cloudinary } if(plugin.settings.folder && result != ""){ result = `${plugin.settings.folder}/${result}` + }else if(!plugin.settings.folder && result != ""){ + result = `${result}` }else if(plugin.settings.folder){ result = `${plugin.settings.folder}`; }else{