From 2cdac3116d2809bbe0167d8c2c56ea159619b3b3 Mon Sep 17 00:00:00 2001 From: Mara Date: Thu, 28 Sep 2023 22:03:19 +0200 Subject: [PATCH] fix(attachments): better optimization for api call use only the commits history to get the lasted update for a attachments if there is no date / an error or if the date is older than the last editing of the file upload prevent two much api call, but add one more api call than the last version, as all image are tested for their last update + uploaded if there is no date so now attachment take 2 API call (test existing + upload) but **not** all attachments have two API call (only the one that need to be uploaded). --- src/GitHub/upload.ts | 17 ++++++++++------- src/i18n/locales/en.json | 5 ----- src/i18n/locales/fr.json | 5 ----- src/settings.ts | 21 --------------------- src/settings/interface.ts | 2 -- src/utils/data_validation_test.ts | 8 +------- 6 files changed, 11 insertions(+), 47 deletions(-) diff --git a/src/GitHub/upload.ts b/src/GitHub/upload.ts index baf7214e..13f78ca3 100644 --- a/src/GitHub/upload.ts +++ b/src/GitHub/upload.ts @@ -34,7 +34,6 @@ import { import { checkEmptyConfiguration, checkIfRepoIsInAnother, - forcePushAttachment, isAttachment, isShared, } from "../utils/data_validation_test"; @@ -570,7 +569,7 @@ export default class Publisher { */ async cleanLinkedImageIfAlreadyInRepo( embedFiles: TFile[], - properties: MonoProperties + properties: MonoProperties, ): Promise { const newLinkedFiles: TFile[] = []; for (const file of embedFiles) { @@ -582,18 +581,22 @@ export default class Publisher { ); const repoFrontmatter = properties.frontmatter; try { - const {status} = await this.octokit.request( - "GET /repos/{owner}/{repo}/contents/{path}", + const {status, data } = await this.octokit.request( + "GET /repos/{owner}/{repo}/commits", { owner: repoFrontmatter.repo.owner, repo: repoFrontmatter.repo.repo, path: imagePath, }); + if (status === 200) { - if (forcePushAttachment(file, properties.settings)) { + const lastEditedInRepo = data[0]?.commit?.committer?.date; + const lastEditedDate = lastEditedInRepo ? new Date(lastEditedInRepo) : undefined; + const lastEditedAttachment = new Date(file.stat.mtime); + //if the file in the vault is newer than the file in the repo, push it + if (lastEditedDate && lastEditedAttachment > lastEditedDate || !lastEditedDate) { newLinkedFiles.push(file); - } else notif({ settings: this.settings }, i18next.t("error.alreadyExists", { file: file.name })); - continue; + } else logs({settings: this.settings}, i18next.t("error.alreadyExists", {file: file.name})); } } catch (e) { newLinkedFiles.push(file); diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index e09deb3f..196c7110 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -30,7 +30,6 @@ }, "shareActiveFile": "Upload single current active note", "shareViewFiles": { - "default": "Github Publisher: Upload {{- viewFile}}", "multiple": { "on": "Upload {{- doc}} to {{- smartKey }}", "other": "Upload to…" @@ -232,9 +231,6 @@ "title": "Default attachment folder" }, "forcePush": { - "desc": "The following extensions will always be sent to the repository, even if they already exist there. \nSeparate extensions with a comma.", - "infoAboutAttachments": "Normally, attachments are only sent if it does not exist on the repository.", - "title": "Force push attachments" }, "links": { "desc": "Allow to edit the links of the embeds, removing entirely the citation, or transform to a simple link", @@ -249,7 +245,6 @@ "notes": "Note (Markdown)", "title": "Embed", "transferImage": { - "desc": "Send attachments embedded in a file to GitHub.", "title": "Transfer attachments" }, "transferMetaFile": { diff --git a/src/i18n/locales/fr.json b/src/i18n/locales/fr.json index 300f1a74..2bcfa3e4 100644 --- a/src/i18n/locales/fr.json +++ b/src/i18n/locales/fr.json @@ -30,7 +30,6 @@ }, "shareActiveFile": "Transférer la note active", "shareViewFiles": { - "default": "Github Publisher : Transférer {{- viewFile}}", "multiple": { "on": "Transférer {{- doc}} vers {{- smartKey }}", "other": "Transférer vers…" @@ -232,9 +231,6 @@ "title": "Dossier de pièces-jointes par défaut" }, "forcePush": { - "desc": "Les extensions suivantes seront toujours envoyés dans le dépôt, même si elles y existent déjà. Séparer les extensions par une virgule.", - "infoAboutAttachments": "Normalement, les pièces-jointes sont envoyées uniquement si elle n'existe pas sur le dépôt.", - "title": "Forcer l'envoie des pièces-jointes" }, "links": { "desc": "Permet d'éditer les liens des embeds, en supprimant entièrement la citation, ou en la transformant en un simple lien.", @@ -249,7 +245,6 @@ "notes": "Note (Markdown)", "title": "Embed", "transferImage": { - "desc": "Transférer les pièces-jointes", "title": "Envoyer les pièces-jointes intégrées dans un fichier dans le dépôt." }, "transferMetaFile": { diff --git a/src/settings.ts b/src/settings.ts index b13d92c0..7956d073 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -802,27 +802,6 @@ export class GithubPublisherSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }); }); - - const forcePushDesc = document.createDocumentFragment(); - forcePushDesc.createEl("div", { text: i18next.t("settings.embed.forcePush.infoAboutAttachments") }); - forcePushDesc.createEl("div", { text: i18next.t("settings.embed.forcePush.desc") }); - - new Setting(this.settingsPage) - .setName(i18next.t("settings.embed.forcePush.title")) - .setDesc(forcePushDesc) - .setClass("mini") - .addTextArea((text) => { - text - .setPlaceholder("pdf, png") - .setValue(embedSettings.forcePushAttachments?.join(", ")) - .onChange(async (value) => { - embedSettings.forcePushAttachments = value - .split(/[,\n]\W*/) - .map((item) => item.trim()) - .filter((item) => item.length > 0); - await this.plugin.saveSettings(); - }); - }); } new Setting(this.settingsPage) diff --git a/src/settings/interface.ts b/src/settings/interface.ts index 5a0ae053..3d7e6e40 100644 --- a/src/settings/interface.ts +++ b/src/settings/interface.ts @@ -112,7 +112,6 @@ export interface GitHubPublisherSettings { } embed: { attachments: boolean; - forcePushAttachments: string[]; keySendFile: string[]; notes: boolean; folder: string; @@ -261,7 +260,6 @@ export const DEFAULT_SETTINGS: GitHubPublisherSettings = { }, embed: { attachments: true, - forcePushAttachments: [], keySendFile: [], notes: false, folder: "", diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index f3402123..ba76bf03 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -386,15 +386,9 @@ export async function verifyRateLimitAPI(octokit: Octokit, settings: GitHubPubli })); } else { new Notice(i18next.t("commands.checkValidity.rateLimit.notLimited", { - remaining: remaining, + remaining, resetTime: time })); } return remaining; -} - -export function forcePushAttachment(file: TFile, settings: GitHubPublisherSettings) { - const forcePushThese = settings.embed.forcePushAttachments; - if (forcePushThese.length === 0) return false; - return forcePushThese.includes(file.extension); } \ No newline at end of file