Skip to content

Commit

Permalink
Merge pull request #14 from jordanhandy/subfolders
Browse files Browse the repository at this point in the history
Allow for specifying asset-type subfolders
  • Loading branch information
jordanhandy authored May 26, 2024
2 parents d7f3b2e + 1bf8d00 commit bfca6d6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 34 deletions.
48 changes: 15 additions & 33 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,7 @@ import objectPath from 'object-path'

// Settings tab import
import CloudinaryUploaderSettingTab from './settings-tab'

//Define Cloudinary Settings
interface CloudinarySettings {
cloudName: string;
uploadPreset: string;
folder: string;
f_auto: boolean;
transformParams: string;
dropUpload: boolean;
clipboardUpload: boolean;
imageUpload: boolean;
audioUpload: boolean;
videoUpload: boolean;
rawUpload: boolean;
}

// Set settings defaults
const DEFAULT_SETTINGS: CloudinarySettings = {
cloudName: null,
uploadPreset: null,
folder: null,
f_auto: false,
transformParams: null,
dropUpload: false,
clipboardUpload: true,
imageUpload: true,
audioUpload: false,
videoUpload: false,
rawUpload: false
};
import { DEFAULT_SETTINGS, CloudinarySettings } from "./settings-tab";
export default class CloudinaryUploader extends Plugin {
settings: CloudinarySettings;

Expand Down Expand Up @@ -89,14 +60,13 @@ export default class CloudinaryUploader extends Plugin {
const randomString = (Math.random() * 10086).toString(36).substr(0, 8)
const pastePlaceText = `![uploading...](${randomString})\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.settings.folder);
formData.append('folder',this.setSubfolder(file));

// Make API call
axios({
Expand Down Expand Up @@ -142,6 +112,19 @@ export default class CloudinaryUploader extends Plugin {
}
}
}

// Set subfolder for upload
private setSubfolder(file : File){
if(file.type.startsWith("image")){
return `${this.settings.folder}/${this.settings.imageSubfolder}`;
}else if(file.type.startsWith("audio")){
return `${this.settings.folder}/${this.settings.audioSubfolder}`;
}else if(file.type.startsWith("video")){
return `${this.settings.folder}/${this.settings.videoSubfolder}`;
}else{
return `${this.settings.folder}/${this.settings.rawSubfolder}`;
}
}
// Function to replace text
private replaceText(editor: Editor, target: string, replacement: string): void {
target = target.trim();
Expand All @@ -166,7 +149,6 @@ export default class CloudinaryUploader extends Plugin {
await this.loadSettings();
this.clearHandlers();
this.setupHandlers();
//this.setupPasteHandler();
this.addSettingTab(new CloudinaryUploaderSettingTab(this.app, this));
}

Expand Down
94 changes: 93 additions & 1 deletion src/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,46 @@ import {

import CloudinaryUploader from './main'

//Define Cloudinary Settings
export interface CloudinarySettings {
cloudName: string;
uploadPreset: string;
folder: string;
f_auto: boolean;
transformParams: string;
dropUpload: boolean;
clipboardUpload: boolean;
imageUpload: boolean;
audioUpload: boolean;
videoUpload: boolean;
rawUpload: boolean;
imageSubfolder: string;
audioSubfolder: string;
videoSubfolder: string;
rawSubfolder: string;
}
export const DEFAULT_SETTINGS: CloudinarySettings = {
cloudName: "",
uploadPreset: "",
folder: "",
f_auto: false,
transformParams: "",
dropUpload: false,
clipboardUpload: true,
imageUpload: true,
audioUpload: false,
videoUpload: false,
rawUpload: false,
imageSubfolder: "",
audioSubfolder: "",
videoSubfolder: "",
rawSubfolder: ""
};
export default class CloudinaryUploaderSettingTab extends PluginSettingTab {

// Set settings defaults


plugin: CloudinaryUploader;
constructor(app: App, plugin: CloudinaryUploader) {
super(app, plugin);
Expand Down Expand Up @@ -53,7 +92,7 @@ export default class CloudinaryUploaderSettingTab extends PluginSettingTab {
})
});
new Setting(containerEl)
.setName("Cloudinary Upload Folder")
.setName("Cloudinary Root Upload Folder")
.setDesc("Folder name to use in Cloudinary. Note, this will be ignored if you have a folder set in your Cloudinary Upload Preset")
.addText((text) => {
text
Expand Down Expand Up @@ -238,5 +277,58 @@ export default class CloudinaryUploaderSettingTab extends PluginSettingTab {
}
})
});
containerEl.createEl("h4", { text: "File Type Subfolders" });
textFragment = document.createDocumentFragment();
textFragment.append("Choose to add subfolders for different asset types. If not specified, will upload all files in the root folder. As mentioned above, this setting will be ignored if you have a default folder set in Cloudinary's settings for your upload preset");
containerEl.createEl("p", { text: textFragment });

new Setting(containerEl)
.setName("Image Subfolder")
.setDesc("Subfolder name for image files")
.addText((text) => {
text
.setPlaceholder("image")
.setValue(this.plugin.settings.imageSubfolder)
.onChange(async (value) => {
this.plugin.settings.imageSubfolder = value;
await this.plugin.saveSettings();
})
});
new Setting(containerEl)
.setName("Audio Subfolder")
.setDesc("Subfolder name for audio files")
.addText((text) => {
text
.setPlaceholder("audio")
.setValue(this.plugin.settings.audioSubfolder)
.onChange(async (value) => {
this.plugin.settings.audioSubfolder = value;
await this.plugin.saveSettings();
})
});
new Setting(containerEl)
.setName("Video Subfolder")
.setDesc("Subfolder name for video files")
.addText((text) => {
text
.setPlaceholder("video")
.setValue(this.plugin.settings.videoSubfolder)
.onChange(async (value) => {
this.plugin.settings.videoSubfolder = value;
await this.plugin.saveSettings();
})
});
new Setting(containerEl)
.setName("Raw Subfolder")
.setDesc("Subfolder name for raw files")
.addText((text) => {
text
.setPlaceholder("raw")
.setValue(this.plugin.settings.rawSubfolder)
.onChange(async (value) => {
this.plugin.settings.rawSubfolder = value;
await this.plugin.saveSettings();
})
});
}
}

0 comments on commit bfca6d6

Please sign in to comment.