@@ -18,9 +18,8 @@ import MetadataStore, {
1818import EventsListener from "./events-listener" ;
1919import { GitHubSyncSettings } from "./settings/settings" ;
2020import Logger from "./logger" ;
21- import { decodeBase64String } from "./utils" ;
21+ import { decodeBase64String , hasTextExtension } from "./utils" ;
2222import GitHubSyncPlugin from "./main" ;
23- import { fileTypeFromBuffer } from "file-type" ;
2423import { BlobReader , Entry , Uint8ArrayWriter , ZipReader } from "@zip.js/zip.js" ;
2524
2625interface 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
0 commit comments