Skip to content

Commit

Permalink
Fix: archive extension correction (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Jul 20, 2024
1 parent d7d6222 commit 3e10abf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/polyfill/stringPoly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
replaceInsensitive(input: string, searchValue: string, replaceValue: string): string {
// https://stackoverflow.com/a/21257041
const idx = input.toLowerCase().indexOf(searchValue.toLowerCase());
return idx === -1
? input
: input.substring(0, idx) + replaceValue + input.substring(idx + searchValue.length);
},
};
6 changes: 5 additions & 1 deletion src/types/files/archives/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export default class Tar extends Archive {
}

getExtension(): string {
// We can't reliably know the extension
for (const ext of Tar.getExtensions()) {
if (this.getFilePath().toLowerCase().endsWith(ext)) {
return ext;
}
}
return path.parse(this.getFilePath()).ext;
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/files/archives/zipX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export default class ZipX extends SevenZip {

// eslint-disable-next-line class-methods-use-this
getExtension(): string {
for (const ext of ZipX.getExtensions()) {
if (this.getFilePath().toLowerCase().endsWith(ext)) {
return ext;
}
}
return path.parse(this.getFilePath()).ext;
}

Expand Down
14 changes: 10 additions & 4 deletions src/types/outputFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path, { ParsedPath } from 'node:path';

import ArrayPoly from '../polyfill/arrayPoly.js';
import fsPoly from '../polyfill/fsPoly.js';
import StringPoly from '../polyfill/stringPoly.js';
import DAT from './dats/dat.js';
import Game from './dats/game.js';
import Release from './dats/release.js';
Expand Down Expand Up @@ -525,12 +526,17 @@ export default class OutputFactory {

// Should leave archived, generate the archive name from the game name
// The regex is to preserve filenames that use 2+ extensions, e.g. "rom.nes.zip"
const extMatch = inputFile.getFilePath().match(/[^.]+((\.[a-zA-Z0-9]+)+)$/);
const ext = extMatch !== null ? extMatch[1] : '';
const oldExtMatch = inputFile.getFilePath().match(/[^.]+((\.[a-zA-Z0-9]+)+)$/);
const oldExt = oldExtMatch !== null ? oldExtMatch[1] : '';
const gameNameWithoutExt = StringPoly.replaceInsensitive(
path.basename(game.getName()) + oldExt,
inputFile.getArchive().getExtension(),
'',
);
return path.format({
...path.parse(game.getName() + ext),
dir: path.dirname(game.getName()),
name: gameNameWithoutExt,
// Use the archive's canonical extension
base: undefined,
ext: inputFile.getArchive().getExtension(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/igir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ describe('with explicit DATs', () => {
path.join(inputTemp, 'roms', 'rar', 'foobar.rar'),
path.join(inputTemp, 'roms', 'zip', 'loremipsum.zip'),
].map(async (inputFile) => {
const junkFile = inputFile.replace(/((\.[a-zA-Z0-9]+)+)$/, '.junk');
const junkFile = inputFile.replace(/((\.[a-zA-Z0-9]+)+)$/, '');
await fsPoly.mv(inputFile, junkFile);
return junkFile;
}));
Expand Down

0 comments on commit 3e10abf

Please sign in to comment.