-
Notifications
You must be signed in to change notification settings - Fork 11
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
feature/#392_Unpack_gz_files_and_#391follow_redirects #405
Changes from 6 commits
71f22c7
1124884
3a13303
e97b0a6
a83e6f6
561c088
6149013
7b963ad
a23642a
1eb6e1b
b119c5e
5f55fb9
8d0d124
9fe5645
88d480a
35aef3f
b1f1b0d
5194dbe
be91eda
729788b
9499a52
d705a2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { strict as assert } from 'assert'; | ||
import * as zlib from 'node:zlib'; | ||
import * as path from 'path'; | ||
|
||
import * as R from '@jvalue/jayvee-execution'; | ||
|
@@ -20,7 +21,6 @@ import { | |
} from '@jvalue/jayvee-execution'; | ||
import { IOType, PrimitiveValuetypes } from '@jvalue/jayvee-language-server'; | ||
import * as JSZip from 'jszip'; | ||
import * as zlib from 'node:zlib'; | ||
|
||
import { | ||
inferFileExtensionFromFileExtensionString, | ||
|
@@ -46,76 +46,43 @@ export class ArchiveInterpreterExecutor extends AbstractBlockExecutor< | |
'archiveType', | ||
PrimitiveValuetypes.Text, | ||
); | ||
let fs: R.Result<R.FileSystem>; | ||
|
||
if (archiveType === 'zip') { | ||
const fs = await this.loadZipFileToInMemoryFileSystem( | ||
archiveFile, | ||
context, | ||
); | ||
if (R.isErr(fs)) { | ||
return fs; | ||
} | ||
return R.ok(fs.right); | ||
} | ||
if (archiveType === 'gz') { | ||
const fs = await this.loadGzFileToInMemoryFileSystem( | ||
archiveFile, | ||
context, | ||
); | ||
if (R.isErr(fs)) { | ||
return fs; | ||
} | ||
return R.ok(fs.right); | ||
fs = await this.loadZipFileToInMemoryFileSystem(archiveFile, context); | ||
} else if (archiveType === 'gz') { | ||
fs = this.loadGzFileToInMemoryFileSystem(archiveFile, context); | ||
} else { | ||
return R.err({ | ||
message: `Archive type is not supported`, | ||
diagnostic: { node: context.getCurrentNode(), property: 'name' }, | ||
}); | ||
} | ||
|
||
return R.err({ | ||
message: `Archive is not a zip-archive`, | ||
diagnostic: { node: context.getCurrentNode(), property: 'name' }, | ||
}); | ||
if (R.isErr(fs)) { | ||
return fs; | ||
} | ||
return R.ok(fs.right); | ||
} | ||
|
||
private async loadGzFileToInMemoryFileSystem( | ||
private loadGzFileToInMemoryFileSystem( | ||
archiveFile: BinaryFile, | ||
context: ExecutionContext, | ||
): Promise<R.Result<FileSystem>> { | ||
): R.Result<FileSystem> { | ||
context.logger.logDebug(`Loading gz file from binary content`); | ||
try { | ||
|
||
const fs = new InMemoryFileSystem(); | ||
const archivedObject = zlib.gunzipSync(archiveFile.content); | ||
|
||
const extNameArchive = path.extname(archiveFile.name); | ||
const fileName = path.basename(archiveFile.name, extNameArchive); | ||
const extName = path.extname(fileName); | ||
|
||
const mimeType = | ||
inferMimeTypeFromContentTypeString(extName) || | ||
MimeType.APPLICATION_OCTET_STREAM; | ||
const fileExtension = | ||
inferFileExtensionFromFileExtensionString(extName) || | ||
FileExtension.NONE; | ||
const file = new BinaryFile( | ||
fileName, | ||
fileExtension, | ||
mimeType, | ||
archivedObject, | ||
); | ||
|
||
const addedFile = fs.putFile( | ||
InMemoryFileSystem.getPathSeparator() + fileName, | ||
file, | ||
); | ||
|
||
assert(addedFile != null); | ||
|
||
return R.ok(fs); | ||
} | ||
catch (error: unknown) { | ||
return R.err({ | ||
message: `Unexpected Error ${ | ||
error instanceof Error ? error.message : JSON.stringify(err) | ||
} occured during processing`, | ||
diagnostic: { node: context.getCurrentNode(), property: 'name' }, | ||
this.createBinaryAndPutFile(fs, archiveFile.name, archivedObject, { | ||
extNameArchive: extNameArchive, | ||
}); | ||
|
||
return R.ok(fs); | ||
} catch (error: unknown) { | ||
return R.err(this.generateErrorObject(context, error)); | ||
} | ||
} | ||
|
||
|
@@ -133,36 +100,47 @@ export class ArchiveInterpreterExecutor extends AbstractBlockExecutor< | |
)) { | ||
if (!archivedObject.dir) { | ||
const content = await archivedObject.async('arraybuffer'); | ||
// Ext incl. leading dot | ||
const extName = path.extname(archivedObject.name); | ||
const fileName = path.basename(archivedObject.name); | ||
const mimeType = | ||
inferMimeTypeFromContentTypeString(extName) || | ||
MimeType.APPLICATION_OCTET_STREAM; | ||
const fileExtension = | ||
inferFileExtensionFromFileExtensionString(extName) || | ||
FileExtension.NONE; | ||
const file = new BinaryFile( | ||
fileName, | ||
fileExtension, | ||
mimeType, | ||
content, | ||
); | ||
const addedFile = fs.putFile( | ||
InMemoryFileSystem.getPathSeparator() + relPath, | ||
file, | ||
); | ||
assert(addedFile != null); | ||
this.createBinaryAndPutFile(fs, archivedObject.name, content, { | ||
relPath: relPath, | ||
}); | ||
} | ||
} | ||
return R.ok(fs); | ||
} catch (error: unknown) { | ||
return R.err({ | ||
message: `Unexpected Error ${ | ||
error instanceof Error ? error.message : JSON.stringify(err) | ||
} occured during processing`, | ||
diagnostic: { node: context.getCurrentNode(), property: 'name' }, | ||
}); | ||
return R.err(this.generateErrorObject(context, error)); | ||
} | ||
} | ||
|
||
private createBinaryAndPutFile( | ||
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. I think there are two problems with this:
Combined, I'd rename the method to 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.
|
||
fs: InMemoryFileSystem, | ||
archiveFileName: string, | ||
content: ArrayBuffer, | ||
options?: { extNameArchive?: string; relPath?: string }, | ||
) { | ||
const fileName = path.basename(archiveFileName, options?.extNameArchive); | ||
const extName = path.extname(fileName); | ||
|
||
const mimeType = | ||
inferMimeTypeFromContentTypeString(extName) || | ||
MimeType.APPLICATION_OCTET_STREAM; | ||
const fileExtension = | ||
inferFileExtensionFromFileExtensionString(extName) || FileExtension.NONE; | ||
const file = new BinaryFile(fileName, fileExtension, mimeType, content); | ||
|
||
const addedFile = fs.putFile( | ||
InMemoryFileSystem.getPathSeparator() + | ||
String(options?.relPath ?? fileName), | ||
file, | ||
); | ||
assert(addedFile != null); | ||
} | ||
|
||
private generateErrorObject(context: ExecutionContext, error: unknown) { | ||
return { | ||
message: `Unexpected Error ${ | ||
error instanceof Error ? error.message : JSON.stringify(err) | ||
} occured during processing`, | ||
diagnostic: { node: context.getCurrentNode(), property: 'name' }, | ||
}; | ||
} | ||
} |
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.
I don't think we need new examples in the Jayvee repository right now, it would be better to move this to the SWC repo.