Skip to content

Commit 26fb540

Browse files
authored
Separate In-Progress & Completed Downloads (#3)
* Separate In-Progress & Completed Downloads * Store completed demos inside of main directory
1 parent 45530f6 commit 26fb540

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/download.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface DownloadableMatch {
1717

1818
const pipeline = util.promisify(stream.pipeline);
1919
const demosDir = process.env['DEMOS_DIR'] || 'demos';
20+
const tempDemosDir = path.join(demosDir, 'temp');
2021

2122
export const gcpdUrlToFilename = (url: string, suffix?: string): string => {
2223
// http://replay129.valve.net/730/003638895521671676017_1102521424.dem.bz2
@@ -36,19 +37,25 @@ export const gcpdUrlToFilename = (url: string, suffix?: string): string => {
3637
export const downloadSaveDemo = async (match: DownloadableMatch): Promise<bigint | null> => {
3738
try {
3839
if (!match.url) throw new Error('Match download URL missing');
39-
await fsx.mkdirp(demosDir);
40-
const filename = path.join(demosDir, gcpdUrlToFilename(match.url, match.type));
41-
const exists = await fsx.exists(filename);
40+
41+
await fsx.mkdirp(tempDemosDir);
42+
const tempFilename = path.join(tempDemosDir, gcpdUrlToFilename(match.url, match.type));
43+
44+
await fsx.mkdirp(demosDir); // redundant, but added in-case the temp directory is changed in the future to not be nested within the demos directory
45+
const completedFilename = path.join(demosDir, gcpdUrlToFilename(match.url, match.type));
46+
47+
const exists = await fsx.exists(completedFilename);
4248
if (!exists) {
4349
L.trace({ url: match.url }, 'Downloading demo');
4450
const resp = await axios.get<stream.Duplex>(match.url, { responseType: 'stream' });
4551
L.trace({ url: match.url }, 'Demo download complete');
46-
await pipeline(resp.data, bz2(), fs.createWriteStream(filename, 'binary'));
47-
L.trace({ filename }, 'Demo saved to file');
48-
await fsp.utimes(filename, match.date, match.date);
49-
L.info({ filename, date: match.date }, 'Demo save complete');
52+
await pipeline(resp.data, bz2(), fs.createWriteStream(tempFilename, 'binary'));
53+
L.trace({ filename: tempFilename }, 'Demo saved to file');
54+
await fsp.rename(tempFilename, completedFilename);
55+
await fsp.utimes(completedFilename, match.date, match.date);
56+
L.info({ filename: completedFilename, date: match.date }, 'Demo save complete');
5057
} else {
51-
L.info({ filename }, 'File already exists, skipping download');
58+
L.info({ filename: completedFilename }, 'File already exists, skipping download');
5259
}
5360
return null;
5461
} catch (err) {

0 commit comments

Comments
 (0)