Skip to content

Commit

Permalink
Merge pull request #101 from Rollphes/develop
Browse files Browse the repository at this point in the history
v1.2.4
  • Loading branch information
Rollphes committed May 12, 2024
2 parents 4b2495e + 6e5bffd commit 2c48733
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "genshin-manager",
"version": "1.2.3",
"version": "1.2.4",
"homepage": "https://rollphes.github.io/genshin-manager/",
"bugs": "https://github.com/Rollphes/genshin-manager/issues",
"main": "dist/index.js",
Expand Down Expand Up @@ -59,20 +59,20 @@
"@types/cheerio": "^0.22.35",
"@types/cli-progress": "^3.11.5",
"@types/html-escaper": "^3.0.2",
"@types/node": "^20.12.2",
"@types/node": "^20.12.11",
"@types/node-cron": "^3.0.11",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^48.2.2",
"eslint-plugin-jsdoc": "^48.2.4",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-simple-import-sort": "^12.0.0",
"eslint-plugin-simple-import-sort": "^12.1.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.8",
"tsc-alias": "^1.8.9",
"tsconfig-paths": "^4.2.0",
"typedoc": "^0.25.12",
"typescript": "^5.4.3"
"typedoc": "^0.25.13",
"typescript": "^5.4.5"
}
}
24 changes: 22 additions & 2 deletions src/models/assets/AudioAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class AudioAssets {
...cvPaths,
`${this.name}.ogg`,
)
if (fs.existsSync(audioCachePath)) {
if (fs.existsSync(audioCachePath) && !this.isOGGCorrupted(audioCachePath)) {
return await fsPromises.readFile(audioCachePath)
} else {
const res = await fetch(this.url, AudioAssets.fetchOption)
Expand Down Expand Up @@ -154,7 +154,7 @@ export class AudioAssets {
...cvPaths,
`${this.name}.ogg`,
)
if (fs.existsSync(audioCachePath)) {
if (fs.existsSync(audioCachePath) && !this.isOGGCorrupted(audioCachePath)) {
return fs.createReadStream(audioCachePath, {
highWaterMark: highWaterMark,
})
Expand All @@ -178,4 +178,24 @@ export class AudioAssets {
})
}
}

/**
* Check if the OGG file is corrupted
* @warning This function is not perfect, it may not be able to detect all corrupted OGG files. because it only checks the last OggS header.
* @param filePath File path
* @returns is OGG file corrupted
*/
private isOGGCorrupted(filePath: string): boolean {
const data = fs.readFileSync(filePath) // There is no footer in the ogg file, so all the data has to be read in.
const lastIndex = data.lastIndexOf('OggS')

if (lastIndex !== -1) {
const headerType = data.readUInt8(lastIndex + 5)

if (headerType === 4) return false
else return true
} else {
return true
}
}
}
49 changes: 47 additions & 2 deletions src/models/assets/ImageAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ImageAssets {
ImageAssets.imageFolderPath,
`${this.name}.png`,
)
if (fs.existsSync(imageCachePath)) {
if (fs.existsSync(imageCachePath) && !this.isPNGCorrupted(imageCachePath)) {
return await fsPromises.readFile(imageCachePath)
} else {
const res = await fetch(this.url, ImageAssets.fetchOption)
Expand Down Expand Up @@ -162,7 +162,7 @@ export class ImageAssets {
ImageAssets.imageFolderPath,
`${this.name}.png`,
)
if (fs.existsSync(imageCachePath)) {
if (fs.existsSync(imageCachePath) && !this.isPNGCorrupted(imageCachePath)) {
return fs.createReadStream(imageCachePath, {
highWaterMark: highWaterMark,
})
Expand All @@ -185,4 +185,49 @@ export class ImageAssets {
})
}
}

/**
* Check if the PNG file is corrupted
* @warning This function is not perfect, so it may not be able to detect all corrupted files. because it only checks the PNG signature and IEnd chunk.
* @param filePath File path
* @returns is PNG file corrupted
*/
private isPNGCorrupted(filePath: string): boolean {
try {
const stats = fs.statSync(filePath)
const fileSize = stats.size

const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]) // PNG signature
const pngIEndSignature = Buffer.from([73, 69, 78, 68]) // PNG IEnd Chunk Type

const signatureBuffer = Buffer.alloc(pngSignature.length) // PNG signature
const iEndBuffer = Buffer.alloc(8) // PNG IEnd Chunk Type and CRC

if (fileSize < signatureBuffer.length + iEndBuffer.length) return true

const fd = fs.openSync(filePath, 'r')

fs.readSync(fd, signatureBuffer, 0, signatureBuffer.length, 0)

fs.readSync(
fd,
iEndBuffer,
0,
iEndBuffer.length,
fileSize - iEndBuffer.length,
)
const iEndSignatureBuffer = iEndBuffer.slice(0, pngIEndSignature.length)

fs.closeSync(fd)

if (
!pngSignature.equals(signatureBuffer) ||
!pngIEndSignature.equals(iEndSignatureBuffer)
)
return true
return false
} catch (e) {
return true
}
}
}

0 comments on commit 2c48733

Please sign in to comment.