Skip to content

Commit

Permalink
feat: pass local file hash around instead of recomputing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Sep 23, 2024
1 parent 76b53d2 commit dc67860
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.72",
"version": "0.1.73",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 14 additions & 9 deletions src/lib/deltas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Delta = { path: string } & (
| {
type: "uploadFile"
size: number
md5Hash?: string
}
| {
type: "createRemoteDirectory"
Expand Down Expand Up @@ -325,19 +326,23 @@ export class Deltas {
currentLocalItem &&
currentLocalItem.type === "file" &&
normalizeLastModifiedMsForComparison(currentLocalItem.lastModified) >
normalizeLastModifiedMsForComparison(currentRemoteItem.lastModified) &&
(await this.sync.localFileSystem.createFileHash({
normalizeLastModifiedMsForComparison(currentRemoteItem.lastModified)
) {
const md5Hash = await this.sync.localFileSystem.createFileHash({
relativePath: path,
algorithm: "md5"
})) !== this.sync.localFileHashes[currentLocalItem.path]
) {
deltas.push({
type: "uploadFile",
path,
size: currentLocalItem.size
})

pathsAdded[path] = true
if (md5Hash !== this.sync.localFileHashes[currentLocalItem.path]) {
deltas.push({
type: "uploadFile",
path,
size: currentLocalItem.size,
md5Hash
})

pathsAdded[path] = true
}
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/lib/filesystems/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,15 @@ export class LocalFileSystem {

/**
* Upload a local file.
* @date 3/2/2024 - 9:43:58 PM
*
* @public
* @async
* @param {{ relativePath: string }} param0
* @param {{ relativePath: string, passedMD5Hash?: string }} param0
* @param {string} param0.relativePath
* @param {string} param0.passedMD5Hash
* @returns {Promise<CloudItem>}
*/
public async upload({ relativePath }: { relativePath: string }): Promise<CloudItem> {
public async upload({ relativePath, passedMD5Hash }: { relativePath: string; passedMD5Hash?: string }): Promise<CloudItem> {
const localPath = pathModule.join(this.sync.syncPair.localPath, relativePath)
const signalKey = `upload:${relativePath}`
const stats = await fs.stat(localPath)
Expand Down Expand Up @@ -791,12 +791,14 @@ export class LocalFileSystem {
throw new Error(`Could not upload ${relativePath}: Parent path not found.`)
}

const hash = await this.createFileHash({
relativePath,
algorithm: "md5"
})
const md5Hash = passedMD5Hash
? passedMD5Hash
: await this.createFileHash({
relativePath,
algorithm: "md5"
})

this.sync.localFileHashes[relativePath] = hash
this.sync.localFileHashes[relativePath] = md5Hash

const item = await this.sync.sdk.cloud().uploadLocalFile({
source: localPath,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,10 @@ export class Tasks {
case "uploadFile": {
try {
const [, stats] = await Promise.all([
this.sync.localFileSystem.upload({ relativePath: delta.path }),
this.sync.localFileSystem.upload({
relativePath: delta.path,
passedMD5Hash: delta.md5Hash
}),
fs.stat(pathModule.join(this.sync.syncPair.localPath, delta.path))
])
const item = this.sync.remoteFileSystem.getDirectoryTreeCache.tree[delta.path]
Expand Down

0 comments on commit dc67860

Please sign in to comment.