Skip to content

Commit

Permalink
Fix: downloading & extracting temp files (#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Aug 13, 2024
1 parent fecdffd commit afd30f6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/modules/datScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default class DATScanner extends Scanner {
await this.progressBar.reset(datFiles.length);

const downloadedDats = await this.downloadDats(datFiles);
await this.progressBar.reset(downloadedDats.length);
const parsedDats = await this.parseDatFiles(downloadedDats);

this.progressBar.logTrace('done scanning DAT files');
Expand All @@ -96,6 +97,7 @@ export default class DATScanner extends Scanner {

try {
this.progressBar.logTrace(`${datFile.toString()}: downloading`);
// TODO(cemmer): these never get deleted?
const downloadedDatFile = await datFile.downloadToTempPath('dat');
this.progressBar.logTrace(`${datFile.toString()}: downloaded to '${downloadedDatFile.toString()}'`);
return await this.getFilesFromPaths(
Expand Down
6 changes: 3 additions & 3 deletions src/polyfill/urlPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export default {
*/
canParse(input: string, base?: string): boolean {
try {
// eslint-disable-next-line no-new
new URL(input, base);
return true;
const url = new URL(input, base);
// Try to detect and ignore Windows drive letters
return process.platform !== 'win32' || url.protocol.length > 2;
} catch {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/files/archives/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default abstract class Archive {
fsPoly.makeLegal(path.basename(entryPath) || path.parse(this.getFilePath()).name),
));

const tempDir = path.dirname(tempFile);
if (!await fsPoly.exists(tempDir)) {
await fsPoly.mkdir(tempDir, { recursive: true });
}

try {
await this.extractEntryToFile(entryPath, tempFile);
return await callback(tempFile);
Expand Down
13 changes: 13 additions & 0 deletions src/types/files/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,19 @@ export default class File implements FileProps {
https.get(this.getFilePath(), {
timeout: 30_000,
}, (res) => {
if (res.statusCode !== undefined
&& res.statusCode >= 300 && res.statusCode < 400
&& res.headers.location
) {
// Handle redirects
File.fileOf({ filePath: res.headers.location })
.then(async (file) => file.downloadToPath(filePath))
.then(resolve)
.catch(reject);
res.destroy();
return;
}

const writeStream = fs.createWriteStream(filePath);
res.pipe(writeStream);
writeStream.on('finish', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/types/files/fileFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import URLPoly from '../../polyfill/urlPoly.js';
import ExpectedError from '../expectedError.js';
import Archive from './archives/archive.js';
import ArchiveEntry from './archives/archiveEntry.js';
Expand Down Expand Up @@ -33,6 +34,10 @@ export default class FileFactory {
fileChecksumBitmask: number = ChecksumBitmask.CRC32,
archiveChecksumBitmask = fileChecksumBitmask,
): Promise<File[]> {
if (URLPoly.canParse(filePath)) {
return [await File.fileOf({ filePath })];
}

if (!FileFactory.isExtensionArchive(filePath)) {
const entries = await this.entriesFromArchiveSignature(filePath, archiveChecksumBitmask);
if (entries !== undefined) {
Expand Down

0 comments on commit afd30f6

Please sign in to comment.