From 7515484965f26d7f453e238e569e09b3ab6a65b1 Mon Sep 17 00:00:00 2001
From: Jordan Handy <6423379+jordanhandy@users.noreply.github.com>
Date: Sun, 26 May 2024 08:38:53 -0400
Subject: [PATCH] add failure message if required fields are not set
---
src/main.ts | 174 +++++++++++++++++++++++++++-------------------------
1 file changed, 89 insertions(+), 85 deletions(-)
diff --git a/src/main.ts b/src/main.ts
index fdbc1e9..58cf8e8 100755
--- a/src/main.ts
+++ b/src/main.ts
@@ -15,113 +15,117 @@ import { DEFAULT_SETTINGS, CloudinarySettings } from "./settings-tab";
export default class CloudinaryUploader extends Plugin {
settings: CloudinarySettings;
- private clearHandlers(){
- this.app.workspace.off('editor-paste',this.pasteHandler);
- this.app.workspace.off('editor-drop',this.dropHandler);
+ private clearHandlers() {
+ this.app.workspace.off('editor-paste', this.pasteHandler);
+ this.app.workspace.off('editor-drop', this.dropHandler);
}
- private setupHandlers(){
- if(this.settings.clipboardUpload){
- this.registerEvent(this.app.workspace.on('editor-paste',this.pasteHandler));
- }else{
- this.app.workspace.off('editor-paste',this.pasteHandler);
+ private setupHandlers() {
+ if (this.settings.clipboardUpload) {
+ this.registerEvent(this.app.workspace.on('editor-paste', this.pasteHandler));
+ } else {
+ this.app.workspace.off('editor-paste', this.pasteHandler);
}
- if(this.settings.dropUpload){
- this.registerEvent(this.app.workspace.on('editor-drop',this.dropHandler));
- }else{
- this.app.workspace.off('editor-drop',this.dropHandler);
+ if (this.settings.dropUpload) {
+ this.registerEvent(this.app.workspace.on('editor-drop', this.dropHandler));
+ } else {
+ this.app.workspace.off('editor-drop', this.dropHandler);
}
}
- private pasteHandler = async(event : ClipboardEvent, editor: Editor)=>{
+ private pasteHandler = async (event: ClipboardEvent, editor: Editor) => {
const { files } = event.clipboardData;
- await this.uploadFiles(files,event,editor); // to fix
+ await this.uploadFiles(files, event, editor); // to fix
}
- private dropHandler = async(event: DragEventInit, editor: Editor) =>{
+ private dropHandler = async (event: DragEventInit, editor: Editor) => {
const { files } = event.dataTransfer;
- await this.uploadFiles(files,event,editor); // to fix
+ await this.uploadFiles(files, event, editor); // to fix
}
- private uploadFiles = async (files: FileList,event,editor) => {
+ private uploadFiles = async (files: FileList, event, editor) => {
- // On paste event, get "files" from clipbaord or drag data
- // If files contain image, video, or audio move to API call
- // if Files empty or does not contain above, then keep default paste behaviour
- if(files.length > 0){
- if((this.settings.audioUpload && files[0].type.startsWith("audio")) ||
+ // On paste event, get "files" from clipbaord or drag data
+ // If files contain image, video, or audio move to API call
+ // if Files empty or does not contain above, then keep default paste behaviour
+ if (files.length > 0) {
+ if (this.settings.cloudName && this.settings.uploadPreset) {
+
+ if ((this.settings.audioUpload && files[0].type.startsWith("audio")) ||
(this.settings.videoUpload && files[0].type.startsWith('video')) ||
(this.settings.imageUpload && files[0].type.startsWith('image')) ||
(this.settings.rawUpload && !files[0].type.startsWith('image')) &&
- !files[0].type.startsWith('audio') && !files[0].type.startsWith('video') ){
- event.preventDefault(); // Prevent default paste behaviour
-
- if (this.settings.cloudName && this.settings.uploadPreset) {
- for (let file of files) {
- const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
- const pastePlaceText = `\n`
- editor.replaceSelection(pastePlaceText) // Generate random string to show on editor screen while API call completes
- // Cloudinary request format
- // Send form data with a file and upload preset
- // Optionally define a folder
- const formData = new FormData();
- formData.append('file',file);
- formData.append('upload_preset',this.settings.uploadPreset);
- formData.append('folder',this.setSubfolder(file));
-
- // Make API call
- axios({
- url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/auto/upload`,
- method: 'POST',
- data: formData
- }).then(res => {
- // Get response public URL of uploaded image
- console.log(res);
- let url = objectPath.get(res.data, 'secure_url')
- let replaceMarkdownText ="";
-
- // Split URL to allow for appending transformations
- if(this.settings.transformParams){
- const splitURL = url.split("/upload/",2);
- url = splitURL[0]+="/upload/"+this.settings.transformParams+"/"+splitURL[1];
- replaceMarkdownText = ``;
- }
- if(this.settings.f_auto){
- const splitURL = url.split("/upload/",2);
- url = splitURL[0]+="/upload/f_auto/"+splitURL[1];
- replaceMarkdownText = ``;
-
- // leave standard of no transformations added
- }else{
- replaceMarkdownText = ``;
- }
- // Change URL format based on content type
- if(files[0].type.startsWith("audio")){
- replaceMarkdownText = `\n`
- }else if(files[0].type.startsWith("video")){
- replaceMarkdownText = `\n`
- }
- // Show MD syntax using uploaded image URL, in Obsidian Editor
- this.replaceText(editor, pastePlaceText, replaceMarkdownText)
- }, err => {
- // Fail otherwise
- new Notice("There was something wrong with the upload. PLease check your cloud name and template name before trying again "+err, 5000)
- console.log(err)
- })
+ !files[0].type.startsWith('audio') && !files[0].type.startsWith('video')) {
+ event.preventDefault(); // Prevent default paste behaviour
+
+ for (let file of files) {
+ const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
+ const pastePlaceText = `\n`
+ editor.replaceSelection(pastePlaceText) // Generate random string to show on editor screen while API call completes
+ // Cloudinary request format
+ // Send form data with a file and upload preset
+ // Optionally define a folder
+ const formData = new FormData();
+ formData.append('file', file);
+ formData.append('upload_preset', this.settings.uploadPreset);
+ formData.append('folder', this.setSubfolder(file));
+
+ // Make API call
+ axios({
+ url: `https://api.cloudinary.com/v1_1/${this.settings.cloudName}/auto/upload`,
+ method: 'POST',
+ data: formData
+ }).then(res => {
+ // Get response public URL of uploaded image
+ console.log(res);
+ let url = objectPath.get(res.data, 'secure_url')
+ let replaceMarkdownText = "";
+
+ // Split URL to allow for appending transformations
+ if (this.settings.transformParams) {
+ const splitURL = url.split("/upload/", 2);
+ url = splitURL[0] += "/upload/" + this.settings.transformParams + "/" + splitURL[1];
+ replaceMarkdownText = ``;
+ }
+ if (this.settings.f_auto) {
+ const splitURL = url.split("/upload/", 2);
+ url = splitURL[0] += "/upload/f_auto/" + splitURL[1];
+ replaceMarkdownText = ``;
+
+ // leave standard of no transformations added
+ } else {
+ replaceMarkdownText = ``;
+ }
+ // Change URL format based on content type
+ if (files[0].type.startsWith("audio")) {
+ replaceMarkdownText = `\n`
+ } else if (files[0].type.startsWith("video")) {
+ replaceMarkdownText = `\n`
+ }
+ // Show MD syntax using uploaded image URL, in Obsidian Editor
+ this.replaceText(editor, pastePlaceText, replaceMarkdownText)
+ }, err => {
+ // Fail otherwise
+ new Notice("There was something wrong with the upload. PLease check your cloud name and template name before trying again " + err, 5000)
+ console.log(err)
+ })
+ }
}
+ } else {
+ // Failure
+ new Notice("It looks like your Cloudinary Cloud Name or Upload Preset were not defined. Uploads to Cloudinary will fail if these are not set. Please visit plugin settings, or disable plugin to stop this error. Falling back to default paste behaviour", 0);
}
+ }
}
-}
-}
// Set subfolder for upload
- private setSubfolder(file : File){
- if(file.type.startsWith("image")){
+ private setSubfolder(file: File) {
+ if (file.type.startsWith("image")) {
return `${this.settings.folder}/${this.settings.imageSubfolder}`;
- }else if(file.type.startsWith("audio")){
+ } else if (file.type.startsWith("audio")) {
return `${this.settings.folder}/${this.settings.audioSubfolder}`;
- }else if(file.type.startsWith("video")){
+ } else if (file.type.startsWith("video")) {
return `${this.settings.folder}/${this.settings.videoSubfolder}`;
- }else{
+ } else {
return `${this.settings.folder}/${this.settings.rawSubfolder}`;
}
}
@@ -129,7 +133,7 @@ export default class CloudinaryUploader extends Plugin {
private replaceText(editor: Editor, target: string, replacement: string): void {
target = target.trim();
let lines = [];
- for (let i = 0; i < editor.lineCount(); i++){
+ for (let i = 0; i < editor.lineCount(); i++) {
lines.push(editor.getLine(i));
}
for (let i = 0; i < lines.length; i++) {