Skip to content

Commit

Permalink
Ignore filename path case
Browse files Browse the repository at this point in the history
- Ignore  filename path case when comparing files in torrent with files on file system
- Mute TypeScript warning about possible null value
  • Loading branch information
nikolay-borzov committed Oct 3, 2020
1 parent fd63e4f commit fe7a719
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ async function cleanTorrentDir({
const { name, files } = parseResult

// Get absolute paths of torrent files
const torrentFiles = files.map((file) => path.join(dirPath, file))
const torrentFiles = files.map((file) =>
path.join(dirPath, file).toLowerCase()
)

// Get files not listed in the torrent
const extraFiles = dirFiles.filter(
(filename) => torrentFiles.indexOf(filename) === -1
(filename) => torrentFiles.indexOf(filename.toLowerCase()) === -1
)

if (!dryRun) {
Expand Down
6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ exports.loadConfig = async function loadConfig(searchFrom, customConfig) {
/** @type {TorrentCleanConfig} */
const mergedConfig = [
// The closer config to `searchFrom` directory is, the higher its priority
...results.map((result) => result.config).reverse(),
...results
.filter(Boolean)
// @ts-ignore `.filter(Boolean)` ensures none of `result` below is null
.map((result) => result.config)
.reverse(),
customConfig,
]
.filter(Boolean)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nikolay-borzov/torrent-clean",
"version": "1.7.1",
"version": "1.7.2",
"description": "Deletes files that are not listed in selected torrent file",
"keywords": [
"torrent",
Expand Down
23 changes: 22 additions & 1 deletion tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function createTestDir(tempDirPath) {
'image2.jpg': '[binary]',
'image2 (Copy).jpg': '[binary]',
},
Set3: {
'image3.jpg': '[binary]',
},
'.edited': {
'image1.jpg': '[binary]',
},
Expand All @@ -70,7 +73,10 @@ async function createStubTorrent() {
const file2 = Buffer.from('[binary]')
file2.name = 'set2/image2.jpg'

return createTorrent([file1, file2], {
const file3 = Buffer.from('[binary]')
file3.name = 'set3/image3.jpg'

return createTorrent([file1, file2, file3], {
name: 'Nature Wallpapers',
})
}
Expand Down Expand Up @@ -126,6 +132,21 @@ test('cleanTorrentDir » should clean directory from extra files', async (t) =>
})
})

test('cleanTorrentDir » should ignore filename path case', async (t) => {
const { tempDir } = t.context

const { torrentId, dirPath } = await createTestContext(tempDir.path)

const expectKept = path.join(dirPath, 'Set3/image3.jpg')

await cleanTorrentDir({
torrentId,
dirPath,
})

t.true(fs.existsSync(expectKept), `"${expectKept}" should exist`)
})

test('cleanTorrentDir » should postpone files deleting if `dryRun` is set to `true`', async (t) => {
const { tempDir } = t.context

Expand Down

0 comments on commit fe7a719

Please sign in to comment.