MCPcopy
hub / github.com/codedogQBY/ReadAny / downloadRemoteFileToPath

Function downloadRemoteFileToPath

packages/core/src/sync/sync-files.ts:113–149  ·  view source on GitHub ↗
(
  backend: ISyncBackend,
  remotePath: string,
  localPath: string,
  onProgress?: (loaded: number, total: number) => void,
)

Source from the content-addressed store, hash-verified

111}
112
113async function downloadRemoteFileToPath(
114 backend: ISyncBackend,
115 remotePath: string,
116 localPath: string,
117 onProgress?: (loaded: number, total: number) => void,
118): Promise<number | null> {
119 const adapter = getSyncAdapter();
120
121 if (backend.getFileToPath) {
122 const tempPath = await makeTempTransferPath(localPath);
123 try {
124 await backend.getFileToPath(remotePath, tempPath, onProgress);
125 const dir = getDirName(localPath);
126 if (dir) await adapter.ensureDir(dir);
127 await adapter.copyFile(tempPath, localPath);
128 return null;
129 } catch (e) {
130 if (!isDirectFileTransferUnsupported(e)) throw e;
131 console.warn(
132 `[Sync] Direct download unsupported for ${remotePath}; falling back to buffered download`,
133 );
134 } finally {
135 try {
136 await adapter.deleteFile(tempPath);
137 } catch {}
138 }
139 }
140
141 const data = backend.getWithProgress
142 ? await backend.getWithProgress(remotePath, onProgress)
143 : await backend.get(remotePath);
144 const dir = getDirName(localPath);
145 if (dir) await adapter.ensureDir(dir);
146 await adapter.writeFileBytes(localPath, data);
147 onProgress?.(data.length, data.length);
148 return data.length;
149}
150
151type BookRow = {
152 id: string;

Callers 3

buildDownloadFileTaskFunction · 0.85
buildDownloadCoverTaskFunction · 0.85
downloadBookFileFunction · 0.85

Calls 11

getSyncAdapterFunction · 0.90
makeTempTransferPathFunction · 0.85
getDirNameFunction · 0.85
getFileToPathMethod · 0.65
ensureDirMethod · 0.65
copyFileMethod · 0.65
deleteFileMethod · 0.65
getWithProgressMethod · 0.65
getMethod · 0.65
writeFileBytesMethod · 0.65

Tested by

no test coverage detected