@@ -17,6 +17,7 @@ export interface DownloadableMatch {
17
17
18
18
const pipeline = util . promisify ( stream . pipeline ) ;
19
19
const demosDir = process . env [ 'DEMOS_DIR' ] || 'demos' ;
20
+ const tempDemosDir = path . join ( demosDir , 'temp' ) ;
20
21
21
22
export const gcpdUrlToFilename = ( url : string , suffix ?: string ) : string => {
22
23
// http://replay129.valve.net/730/003638895521671676017_1102521424.dem.bz2
@@ -36,19 +37,25 @@ export const gcpdUrlToFilename = (url: string, suffix?: string): string => {
36
37
export const downloadSaveDemo = async ( match : DownloadableMatch ) : Promise < bigint | null > => {
37
38
try {
38
39
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 ) ;
42
48
if ( ! exists ) {
43
49
L . trace ( { url : match . url } , 'Downloading demo' ) ;
44
50
const resp = await axios . get < stream . Duplex > ( match . url , { responseType : 'stream' } ) ;
45
51
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' ) ;
50
57
} else {
51
- L . info ( { filename } , 'File already exists, skipping download' ) ;
58
+ L . info ( { filename : completedFilename } , 'File already exists, skipping download' ) ;
52
59
}
53
60
return null ;
54
61
} catch ( err ) {
0 commit comments