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" } diff --git a/src/commands/utils.ts b/src/commands/utils.ts index 2166d96..699e09d 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 = []; @@ -85,7 +111,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 +129,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 +143,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 @@ -164,26 +195,37 @@ 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 != ""){ + result = `${result}` + }else if(plugin.settings.folder){ + result = `${plugin.settings.folder}`; + }else{ + result =""; + } + return result; } diff --git a/src/main.ts b/src/main.ts index aa51848..17e5f4c 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, uploadAllNotes } from "./commands/utils"; // Settings tab import @@ -25,28 +25,50 @@ 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); } } }); 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: () => { - const files = this.app.vault.getMarkdownFiles() - if (this.settings.ignoreWarnings) { - for (let file of files) { - uploadCurrentNoteFiles(file,this); + 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) { + console.warn(msg); + } + } + + } + } if (!errorFlag) { + new Notice("The operation is complete. No errors to report", 0); } + }); - } else { - uploadNoteModal(undefined, 'note',this); - } - + }else{ + uploadNoteModal(undefined,'note',this); } + } }); this.addCommand({ id: "upload-all-media-assets-cloudinary", @@ -56,11 +78,12 @@ 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 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 {