Skip to content

Commit 5617d1a

Browse files
authored
fix: preserve local files with extensions on re-init (#32)
1 parent 5623cde commit 5617d1a

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,22 +263,15 @@ export default class DownloadQueue {
263263
}
264264

265265
// Delete any files that don't have a spec
266-
const orphanedFiles = dirFilenames.filter(
267-
filename => !this.specs.some(spec => spec.id === filename)
266+
const orphanedFiles = dirFilenames.map(splitFilenameFromExtension).filter(
267+
// Remember that spec.id doesn't have an extension! So use basename.
268+
([basename]) => !this.specs.some(spec => spec.id === basename)
268269
);
269270
if (orphanedFiles.length) {
270271
await Promise.all(
271-
orphanedFiles.map(filename => {
272+
orphanedFiles.map(([basename, ext]) => {
272273
try {
273-
const parts = filename.split(".");
274-
275-
if (parts.length > 1) {
276-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
277-
const extension = parts.pop()!;
278-
279-
return RNFS.unlink(this.pathFromId(parts.join("."), extension));
280-
}
281-
return RNFS.unlink(this.pathFromId(filename, ""));
274+
return RNFS.unlink(this.pathFromId(basename, ext));
282275
} catch {
283276
// Ignore errors
284277
}
@@ -910,6 +903,18 @@ export default class DownloadQueue {
910903
}
911904
}
912905

906+
function splitFilenameFromExtension(filename: string): [string, string] {
907+
const parts = filename.split(".");
908+
909+
if (parts.length > 1) {
910+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
911+
const extension = parts.pop()!;
912+
913+
return [parts.join("."), extension];
914+
}
915+
return [filename, ""];
916+
}
917+
913918
function roundToNextMinute(timestamp: number) {
914919
return Math.ceil(timestamp / 60000) * 60000;
915920
}

test/index.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe("DownloadQueue", () => {
266266
expect(unlink).toHaveBeenCalledTimes(2);
267267
});
268268

269-
it("doesn't delete files that have specs on init", async () => {
269+
it("doesn't delete files without extensions that have specs on init", async () => {
270270
const queue = new DownloadQueue();
271271

272272
(readdir as jest.Mock).mockImplementation(() => ["foo", "bar"]);
@@ -286,6 +286,26 @@ describe("DownloadQueue", () => {
286286
expect(unlink).toHaveBeenCalledTimes(1);
287287
});
288288

289+
it("doesn't delete files with extensions that have specs on init", async () => {
290+
const queue = new DownloadQueue();
291+
292+
(readdir as jest.Mock).mockImplementation(() => ["foo.mp3", "bar.mp3"]);
293+
294+
await kvfs.write("/mydomain/foo", {
295+
id: "foo",
296+
url: "http://foo.com/a.mp3",
297+
path: `${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo.mp3`,
298+
createTime: Date.now() - 1000,
299+
});
300+
await queue.init({ domain: "mydomain" });
301+
expect(unlink).toHaveBeenCalledWith(
302+
expect.stringMatching(
303+
`${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/bar.mp3`
304+
)
305+
);
306+
expect(unlink).toHaveBeenCalledTimes(1);
307+
});
308+
289309
it("revives still-downloading specs from previous launches", async () => {
290310
const queue = new DownloadQueue();
291311
const handlers: DownloadQueueHandlers = {

0 commit comments

Comments
 (0)