-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48a3988
commit 173a834
Showing
5 changed files
with
97 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {app, protocol} from 'electron'; | ||
import pathUtil from 'path'; | ||
import fs from 'fs'; | ||
import {promisify} from 'util'; | ||
import {brotliDecompress} from 'zlib'; | ||
import {staticDir} from './environment'; | ||
|
||
const readFile = promisify(fs.readFile); | ||
const decompress = promisify(brotliDecompress); | ||
|
||
protocol.registerSchemesAsPrivileged([ | ||
{ | ||
scheme: 'tw-library-files', | ||
privileges: { | ||
supportFetchAPI: true | ||
} | ||
} | ||
]); | ||
|
||
const mimeTypes = new Map(); | ||
mimeTypes.set('wav', 'audio/wav'); | ||
mimeTypes.set('svg', 'image/svg+xml'); | ||
mimeTypes.set('png', 'image/png'); | ||
|
||
const decompressAsset = async (md5ext) => { | ||
const extension = md5ext.split('.')[1]; | ||
if (!mimeTypes.has(extension)) { | ||
throw new Error('Unknown extension: ' + extension); | ||
} | ||
const baseDirectory = pathUtil.join(staticDir, 'library-files/'); | ||
const compressedFile = pathUtil.join(baseDirectory, `${md5ext}.br`); | ||
if (!compressedFile.startsWith(baseDirectory)) { | ||
throw new Error('Path traversal'); | ||
} | ||
const compressedData = await readFile(compressedFile); | ||
const decompressed = await decompress(compressedData); | ||
return { | ||
data: decompressed, | ||
type: mimeTypes.get(extension) | ||
}; | ||
}; | ||
|
||
app.whenReady().then(() => { | ||
protocol.registerBufferProtocol('tw-library-files', (request, callback) => { | ||
const md5ext = new URL(request.url).pathname; | ||
decompressAsset(md5ext) | ||
.then((data) => { | ||
callback({ | ||
data: data.data, | ||
mimeType: data.type | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.error('Cannot read library file', err); | ||
callback({ | ||
statusCode: 404 | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters