-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: extractVvpp周りのTODOを解決 #2499
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1d48319
refactor!: extractVvpp周りのTODOを解決
Hiroshiba 58acd6a
refactor: helperは要らなそう
Hiroshiba 76225fa
fix: やっぱりvvpppがあるとこに
Hiroshiba 35a0511
やっぱりtmpDirを渡すように
Hiroshiba 194c6fb
chore: 戻す
Hiroshiba 2d8dd22
chore: temp -> tmp
Hiroshiba 30ed3b7
fix: 出力ファイルのパスをoutputFilePathに変更
Hiroshiba d573603
refactor: createZipFile関数内の変数名をsevenZipBinに変更
Hiroshiba 9ee5f33
test: マニフェスト存在確認
Hiroshiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,6 @@ import { createLogger } from "@/helpers/log"; | |
|
||
const log = createLogger("vvppFile"); | ||
|
||
// https://www.garykessler.net/library/file_sigs.html#:~:text=7-zip%20compressed%20file | ||
const SEVEN_ZIP_MAGIC_NUMBER = Buffer.from([ | ||
0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c, | ||
]); | ||
|
||
const ZIP_MAGIC_NUMBER = Buffer.from([0x50, 0x4b, 0x03, 0x04]); | ||
|
||
/** VVPPファイルが分割されている場合、それらのファイルを取得する */ | ||
async function getArchiveFileParts( | ||
vvppLikeFilePath: string, | ||
|
@@ -61,21 +54,14 @@ async function getArchiveFileParts( | |
|
||
/** 分割されているVVPPファイルを連結して返す */ | ||
async function concatenateVvppFiles( | ||
format: "zip" | "7z", | ||
archiveFileParts: string[], | ||
outputFilePath: string, | ||
) { | ||
// -siオプションでの7z解凍はサポートされていないため、 | ||
// ファイルを連結した一次ファイルを作成し、それを7zで解凍する。 | ||
log.info(`Concatenating ${archiveFileParts.length} files...`); | ||
const tmpConcatenatedFile = path.join( | ||
app.getPath("temp"), // TODO: archiveFilePartsと同じディレクトリにしてappの依存をなくす | ||
`vvpp-${new Date().getTime()}.${format}`, | ||
); | ||
log.info("Temporary file:", tmpConcatenatedFile); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
if (!tmpConcatenatedFile) throw new Error("tmpFile is undefined"); | ||
const inputStreams = archiveFileParts.map((f) => fs.createReadStream(f)); | ||
const outputStream = fs.createWriteStream(tmpConcatenatedFile); | ||
const outputStream = fs.createWriteStream(outputFilePath); | ||
new MultiStream(inputStreams) | ||
.pipe(outputStream) | ||
.on("close", () => { | ||
|
@@ -85,7 +71,6 @@ async function concatenateVvppFiles( | |
.on("error", reject); | ||
}); | ||
log.info("Concatenated"); | ||
return tmpConcatenatedFile; | ||
} | ||
|
||
/** 7zでファイルを解凍する */ | ||
|
@@ -107,13 +92,7 @@ async function unarchive( | |
"-bsp1", // 進捗出力 | ||
]; | ||
|
||
let sevenZipPath = import.meta.env.VITE_7Z_BIN_NAME; | ||
if (!sevenZipPath) { | ||
throw new Error("7z path is not defined"); | ||
} | ||
if (import.meta.env.PROD) { | ||
sevenZipPath = path.join(path.dirname(app.getPath("exe")), sevenZipPath); // TODO: helperに移動してappの依存をなくす | ||
} | ||
const sevenZipPath = getSevenZipPath(); | ||
log.info("Spawning 7z:", sevenZipPath, args.join(" ")); | ||
await new Promise<void>((resolve, reject) => { | ||
const child = spawn(sevenZipPath, args, { | ||
|
@@ -152,13 +131,24 @@ async function unarchive( | |
// FIXME: rejectが2回呼ばれることがある | ||
child.on("error", reject); | ||
}); | ||
|
||
function getSevenZipPath() { | ||
let sevenZipPath = import.meta.env.VITE_7Z_BIN_NAME; | ||
if (!sevenZipPath) { | ||
throw new Error("7z path is not defined"); | ||
} | ||
if (import.meta.env.PROD) { | ||
sevenZipPath = path.join(path.dirname(app.getPath("exe")), sevenZipPath); | ||
} | ||
return sevenZipPath; | ||
} | ||
} | ||
|
||
export async function extractVvpp( | ||
vvppLikeFilePath: string, | ||
vvppEngineDir: string, // TODO: payload objectに変える | ||
payload: { vvppLikeFilePath: string; vvppEngineDir: string; tmpDir: string }, | ||
callbacks?: { onProgress?: ProgressCallback }, | ||
): Promise<{ outputDir: string; manifest: MinimumEngineManifestType }> { | ||
const { vvppLikeFilePath, vvppEngineDir, tmpDir } = payload; | ||
callbacks?.onProgress?.({ progress: 0 }); | ||
|
||
const nonce = new Date().getTime().toString(); | ||
|
@@ -177,10 +167,12 @@ export async function extractVvpp( | |
let archiveFile: string; | ||
try { | ||
if (archiveFileParts.length > 1) { | ||
tmpConcatenatedFile = await concatenateVvppFiles( | ||
format, | ||
archiveFileParts, | ||
); | ||
// -siオプションでの7z解凍はサポートされていないため、 | ||
// ファイルを連結した一次ファイルを作成し、それを7zで解凍する。 | ||
tmpConcatenatedFile = createTmpConcatenatedFilePath(); | ||
log.info("Temporary file:", tmpConcatenatedFile); | ||
|
||
await concatenateVvppFiles(archiveFileParts, tmpConcatenatedFile); | ||
archiveFile = tmpConcatenatedFile; | ||
} else { | ||
archiveFile = archiveFileParts[0]; | ||
|
@@ -214,6 +206,10 @@ export async function extractVvpp( | |
} | ||
throw e; | ||
} | ||
|
||
function createTmpConcatenatedFilePath(): string { | ||
return path.join(tmpDir, `vvpp-${new Date().getTime()}.${format}`); | ||
} | ||
} | ||
|
||
async function detectFileFormat( | ||
|
@@ -225,6 +221,13 @@ async function detectFileFormat( | |
await file.read(buffer, 0, 8, 0); | ||
await file.close(); | ||
|
||
// https://www.garykessler.net/library/file_sigs.html#:~:text=7-zip%20compressed%20file | ||
const SEVEN_ZIP_MAGIC_NUMBER = Buffer.from([ | ||
0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c, | ||
]); | ||
|
||
const ZIP_MAGIC_NUMBER = Buffer.from([0x50, 0x4b, 0x03, 0x04]); | ||
Comment on lines
+224
to
+229
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. スコープは短いほど良いので関数内に移動・・・ |
||
|
||
if (buffer.compare(SEVEN_ZIP_MAGIC_NUMBER, 0, 6, 0, 6) === 0) { | ||
return "7z"; | ||
} else if (buffer.compare(ZIP_MAGIC_NUMBER, 0, 4, 0, 4) === 0) { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vvpppを結合したものができるファイル、やっぱりelectronのtempディレクトリに置くようにしました。
これが一番明確だなぁと。