Skip to content

Commit 1a2eb64

Browse files
committed
Simplify logic that determines if a file is text or binary
1 parent 5ab20cf commit 1a2eb64

File tree

4 files changed

+40
-109
lines changed

4 files changed

+40
-109
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@uiw/react-codemirror": "^4.23.8",
3737
"@zip.js/zip.js": "^2.7.60",
3838
"codemirror": "^6.0.1",
39-
"file-type": "^20.4.1",
4039
"react": "^19.0.0",
4140
"react-dom": "^19.0.0"
4241
}

pnpm-lock.yaml

Lines changed: 0 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sync-manager.ts

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import MetadataStore, {
1818
import EventsListener from "./events-listener";
1919
import { GitHubSyncSettings } from "./settings/settings";
2020
import Logger from "./logger";
21-
import { decodeBase64String } from "./utils";
21+
import { decodeBase64String, hasTextExtension } from "./utils";
2222
import GitHubSyncPlugin from "./main";
23-
import { fileTypeFromBuffer } from "file-type";
2423
import { BlobReader, Entry, Uint8ArrayWriter, ZipReader } from "@zip.js/zip.js";
2524

2625
interface SyncAction {
@@ -742,45 +741,25 @@ export default class SyncManager {
742741
Object.keys(treeFiles)
743742
.filter((filePath: string) => treeFiles[filePath].content)
744743
.map(async (filePath: string) => {
745-
// Some Markdown or JSON files might grow to be quite big, that makes it
746-
// impossible for the file-type library to guess their file type.
747-
// It also increases the amount of memory used by A LOT and might cause
748-
// issues in devices with low memory if there are lots of files to check.
749-
//
750744
// I don't fully trust file extensions as they're not completely reliable
751745
// to determine the file type, though I feel it's ok to compromise and rely
752746
// on them if it makes the plugin handle upload better on certain devices.
753-
if (filePath.endsWith(".md") || filePath.endsWith(".json")) {
754-
this.metadataStore.data.files[filePath].sha =
755-
await this.calculateSHA(filePath);
747+
if (hasTextExtension(filePath)) {
748+
const sha = await this.calculateSHA(filePath);
749+
this.metadataStore.data.files[filePath].sha = sha;
756750
return;
757751
}
752+
753+
// We can't upload binary files by setting the content of a tree item,
754+
// we first need to create a Git blob by uploading the file, then
755+
// we must update the tree item to point the SHA to the blob we just created.
758756
const buffer = await this.vault.adapter.readBinary(filePath);
759-
const fileType = await fileTypeFromBuffer(buffer);
760-
let newSha = "";
761-
if (
762-
// We can't determine the file type
763-
fileType === undefined ||
764-
// This is not a text file
765-
!fileType.mime.startsWith("text/") ||
766-
// Neither a json file
767-
fileType.mime !== "application/json"
768-
) {
769-
// We treat this file as a binary file. We can't upload these setting the content
770-
// of a tree item, we first need to create a Git blob by uploading the file, then
771-
// we must update the tree item to point the SHA to the blob we just created.
772-
const hash = arrayBufferToBase64(buffer);
773-
const { sha } = await this.client.createBlob(hash);
774-
treeFiles[filePath].sha = sha;
775-
// Can't have both sha and content set, so we delete it
776-
delete treeFiles[filePath].content;
777-
newSha = sha;
778-
} else {
779-
// File is text, we can upload the content directly
780-
// so we just calculate the new SHA to keep track of it
781-
newSha = await this.calculateSHA(filePath);
782-
}
783-
this.metadataStore.data.files[filePath].sha = newSha;
757+
const hash = arrayBufferToBase64(buffer);
758+
const { sha } = await this.client.createBlob(hash);
759+
treeFiles[filePath].sha = sha;
760+
// Can't have both sha and content set, so we delete it
761+
delete treeFiles[filePath].content;
762+
this.metadataStore.data.files[filePath].sha = sha;
784763
}),
785764
);
786765

src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { base64ToArrayBuffer } from "obsidian";
22

3+
const TEXT_EXTENSIONS = [
4+
".css",
5+
".md",
6+
".json",
7+
".txt",
8+
".csv",
9+
".js",
10+
".log",
11+
] as const;
12+
313
/**
414
* Decodes a base64 encoded string, this properly
515
* handles emojis and other non ASCII chars.
@@ -37,3 +47,19 @@ export async function copyToClipboard(text: string) {
3747
document.body.removeChild(textarea);
3848
}
3949
}
50+
51+
/**
52+
* Checks if a file path has one of the predefined text extensions.
53+
* This is a best guess at best.
54+
*
55+
* @param filePath The path of the file to check
56+
* @returns True if the file has a text extension, false otherwise
57+
*/
58+
export function hasTextExtension(filePath: string) {
59+
for (const extension in TEXT_EXTENSIONS) {
60+
if (filePath.endsWith(extension)) {
61+
return true;
62+
}
63+
}
64+
return false;
65+
}

0 commit comments

Comments
 (0)