MCPcopy
hub / github.com/remotely-save/remotely-save / tryDuplicateFileForSameSizes

Function tryDuplicateFileForSameSizes

pro/src/conflictLogic.ts:242–299  ·  view source on GitHub ↗

* 1. download remote * 2. compare * 3. if the same, update local but not upload * 4. if not the same, rename local and save remote

(
  key: string,
  key2: string,
  fsLocal: FakeFs,
  fsRemote: FakeFs,
  uploadCallback: (entity: Entity | undefined) => Promise<any>,
  downloadCallback: (entity: Entity | undefined) => Promise<any>
)

Source from the content-addressed store, hash-verified

240 * 4. if not the same, rename local and save remote
241 */
242async function tryDuplicateFileForSameSizes(
243 key: string,
244 key2: string,
245 fsLocal: FakeFs,
246 fsRemote: FakeFs,
247 uploadCallback: (entity: Entity | undefined) => Promise<any>,
248 downloadCallback: (entity: Entity | undefined) => Promise<any>
249) {
250 console.debug(`tryDuplicateFileForSameSizes: ${key}`);
251
252 // 1. download
253 const remoteContent = await fsRemote.readFile(key);
254
255 // 2. compare
256 const localContent = await fsLocal.readFile(key);
257 const eq = arraysAreEqual(localContent, remoteContent);
258
259 if (eq) {
260 // 3. if the same, update local but not upload
261 // read meta of remote, as if we have downloaded the file
262 console.debug(`tryDuplicateFileForSameSizes: ${key} content equal`);
263 const entityRemote = await fsRemote.stat(key);
264
265 // write
266 const downloadResultEntity = await fsLocal.writeFile(
267 key,
268 remoteContent,
269 entityRemote.mtimeCli ?? Date.now(),
270 entityRemote.mtimeCli ?? Date.now()
271 );
272 await downloadCallback(downloadResultEntity);
273
274 // no uploadCallback here
275 } else {
276 // 4. if not the same, rename local and save remote
277 console.debug(`tryDuplicateFileForSameSizes: ${key} content not equal`);
278
279 await fsLocal.rename(key, key2);
280
281 const entityRemote = await fsRemote.stat(key);
282 const downloadResultEntity = await fsLocal.writeFile(
283 key,
284 remoteContent,
285 entityRemote.mtimeCli ?? Date.now(),
286 entityRemote.mtimeCli ?? Date.now()
287 );
288 await downloadCallback(downloadResultEntity);
289
290 const entityLocal = await fsLocal.stat(key2); // key2 here!
291 const uploadResultEntity = await fsRemote.writeFile(
292 key2, // key2 here!
293 localContent,
294 entityLocal.mtimeCli ?? Date.now(),
295 entityLocal.mtimeCli ?? Date.now()
296 );
297 await uploadCallback(uploadResultEntity);
298 }
299}

Callers 1

tryDuplicateFileFunction · 0.85

Calls 5

arraysAreEqualFunction · 0.85
readFileMethod · 0.45
statMethod · 0.45
writeFileMethod · 0.45
renameMethod · 0.45

Tested by

no test coverage detected