-
Notifications
You must be signed in to change notification settings - Fork 273
add 7z handler #552
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
Open
neko782
wants to merge
2
commits into
p2r3:master
Choose a base branch
from
neko782:7z
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add 7z handler #552
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,174 @@ | ||
| // file: 7z.ts | ||
|
|
||
| import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; | ||
| import CommonFormats, { Category } from "src/CommonFormats.ts"; | ||
| import SevenZip from "7z-wasm"; | ||
| import mime from "mime"; | ||
| import normalizeMimeType from "src/normalizeMimeType.ts"; | ||
|
|
||
| const defaultSevenZipOptions = { | ||
| locateFile: () => "/convert/wasm/7zz.wasm" | ||
| } | ||
|
|
||
| class sevenZipHandler implements FormatHandler { | ||
|
|
||
| public name: string = "sevenZip"; | ||
| public supportedFormats: FileFormat[] = []; | ||
| public ready: boolean = false; | ||
|
|
||
| public supportAnyInput: boolean = true; | ||
|
|
||
| #tarCompressedFormats: string[] = []; | ||
|
|
||
| async init () { | ||
| this.supportedFormats = []; | ||
| this.#tarCompressedFormats = []; | ||
|
|
||
| const stdout: number[] = []; | ||
| const sevenZip = await SevenZip({ | ||
| ...defaultSevenZipOptions, | ||
| stdout: (c) => { | ||
| stdout.push(c); | ||
| }, | ||
| }); | ||
|
|
||
| sevenZip.callMain(["i"]); | ||
|
|
||
| const text = new TextDecoder().decode(new Uint8Array(stdout)); | ||
|
|
||
| // no codecs for now | ||
| const formatsText = text.match(/\n\n\nFormats:\n(.*?)\n\n/s); | ||
| if (!formatsText) throw new Error("7zz output did not have any formats"); | ||
| const formatLines = formatsText[1].split("\n"); | ||
|
|
||
| // this will totally break in future 7z versions but its the only way | ||
| for (const formatLine of formatLines) { | ||
| // 7zz i gives more than 1 extension, but i dont think we will | ||
| // need those as they are mostly aliases and thats renamehandler's job. | ||
| // also we cant faithfully parse more than 1 extension because there is no | ||
| // way to know where extensions stop and weird signature stuff begins | ||
| const [flags, name, extension, ...extra] = formatLine.trim().split(/ +/); | ||
|
|
||
| if (name === "Hash") continue; | ||
| const mimeType = normalizeMimeType(mime.getType(extension) || `application/${extension}`); | ||
| let displayName = `${name} archive`; | ||
| let format = extension; | ||
|
|
||
| if (extra.includes("(.tar)")) { | ||
| // compressed formats will be only tar for now | ||
| this.#tarCompressedFormats.push(name); | ||
| displayName = `${name} compressed tar archive`; | ||
| format = `tar.${extension}`; | ||
| } | ||
|
|
||
| this.supportedFormats.push({ | ||
| name: displayName, | ||
| format, | ||
| extension, | ||
| mime: mimeType, | ||
| from: true, | ||
| to: flags.includes("C"), | ||
| internal: name, | ||
| category: Category.ARCHIVE, | ||
| lossless: false, // archive metadata is too complicated | ||
| }); | ||
| } | ||
|
|
||
| // push zip and tar up the list | ||
| const priority = ["tar", "zip"]; | ||
| const prioritized = []; | ||
| for (const format of priority) { | ||
| prioritized.push(this.supportedFormats.find(f => f.internal === format)!); | ||
| } | ||
| this.supportedFormats = [ | ||
| ...prioritized, | ||
| ...this.supportedFormats.filter(f => !priority.includes(f.internal)) | ||
| ]; | ||
|
|
||
| this.ready = true; | ||
| } | ||
|
|
||
| async doConvert ( | ||
| inputFiles: FileData[], | ||
| inputFormat: FileFormat, | ||
| outputFormat: FileFormat | ||
| ): Promise<FileData[]> { | ||
| const outputFiles: FileData[] = []; | ||
|
|
||
| if (!this.supportedFormats.some(format => format.to && format.internal === outputFormat.internal)) { | ||
| throw new Error(`sevenZipHandler cannot convert to ${outputFormat.mime}`); | ||
| } | ||
|
|
||
| // handle compressed tars | ||
| if (this.#tarCompressedFormats.includes(inputFormat.internal) | ||
| || this.#tarCompressedFormats.includes(outputFormat.internal)) { | ||
|
|
||
| if (outputFormat.internal === "tar") { | ||
| for (const inputFile of inputFiles) { | ||
| const sevenZip = await SevenZip(defaultSevenZipOptions); | ||
|
|
||
| sevenZip.FS.writeFile(inputFile.name, inputFile.bytes); | ||
| sevenZip.callMain(["x", inputFile.name]); | ||
|
|
||
| const name = inputFile.name.replace(/\.[^.]+$/, ""); | ||
| const bytes = sevenZip.FS.readFile(name); | ||
| outputFiles.push({ bytes, name }); | ||
| } | ||
| } else if (inputFormat.internal === "tar") { | ||
| for (const inputFile of inputFiles) { | ||
| const sevenZip = await SevenZip(defaultSevenZipOptions); | ||
| sevenZip.FS.writeFile(inputFile.name, inputFile.bytes); | ||
|
|
||
| const name = inputFile.name + `.${outputFormat.extension}`; | ||
| sevenZip.callMain(["a", name, inputFile.name]); | ||
|
|
||
| const bytes = sevenZip.FS.readFile(name); | ||
| outputFiles.push({ bytes, name }); | ||
| } | ||
| } else { | ||
| throw new Error(`sevenZipHandler cannot convert from ${inputFormat.mime} to ${outputFormat.mime}`); | ||
| } | ||
|
|
||
| return outputFiles; | ||
| } | ||
|
|
||
| if (this.supportedFormats.some(format => format.internal === inputFormat.internal)) { | ||
| for (const inputFile of inputFiles) { | ||
| const sevenZip = await SevenZip(defaultSevenZipOptions); | ||
|
|
||
| sevenZip.FS.writeFile(inputFile.name, inputFile.bytes); | ||
| sevenZip.callMain(["x", inputFile.name, `-odata`]); | ||
|
|
||
| const name = inputFile.name.replace(/\.[^.]+$/, "") + `.${outputFormat.extension}`; | ||
| sevenZip.FS.chdir("data"); // we need to preserve the structure of the input archive | ||
| sevenZip.callMain(["a", "../" + name]); | ||
| sevenZip.FS.chdir(".."); | ||
|
|
||
| const bytes = sevenZip.FS.readFile(name); | ||
| outputFiles.push({ bytes, name }); | ||
| } | ||
| } else { | ||
| const sevenZip = await SevenZip(defaultSevenZipOptions); | ||
|
|
||
| sevenZip.FS.mkdir("data"); | ||
| sevenZip.FS.chdir("data"); | ||
| for (const inputFile of inputFiles) { | ||
| sevenZip.FS.writeFile(inputFile.name, inputFile.bytes); | ||
| } | ||
|
|
||
| const name = inputFiles.length === 1 ? | ||
| inputFiles[0].name + `.${outputFormat.extension}` | ||
| : `archive.${outputFormat.extension}`; | ||
| sevenZip.callMain(["a", "../" + name]); | ||
| sevenZip.FS.chdir(".."); | ||
|
|
||
| const bytes = sevenZip.FS.readFile(name); | ||
| outputFiles.push({ bytes, name }); | ||
| } | ||
|
|
||
| return outputFiles; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| export default sevenZipHandler; | ||
Oops, something went wrong.
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.
just for some better routing