feat(file): Added functionality to retrieve magic bytes from files and detect file types using magic bytes.

This commit is contained in:
2024-11-18 15:07:46 +01:00
parent c7f0c97341
commit 383a5204f4
7 changed files with 6027 additions and 4273 deletions

View File

@@ -232,6 +232,7 @@ export class Bucket {
if (typeArg === 'webstream') {
return (await duplexStream.getWebStreams()).readable;
}
throw new Error('unknown typeArg');
}
/**
@@ -437,4 +438,28 @@ export class Bucket {
const response = await this.smartbucketRef.s3Client.send(command);
return response.Contents.length > 0;
}
public async getMagicBytes(optionsArg: { path: string; length: number }): Promise<Buffer> {
try {
const command = new plugins.s3.GetObjectCommand({
Bucket: this.name,
Key: optionsArg.path,
Range: `bytes=0-${optionsArg.length - 1}`,
});
const response = await this.smartbucketRef.s3Client.send(command);
const chunks = [];
const stream = response.Body as any; // SdkStreamMixin includes readable stream
for await (const chunk of stream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
} catch (error) {
console.error(
`Error retrieving magic bytes from object at path '${optionsArg.path}' in bucket '${this.name}':`,
error
);
throw error;
}
}
}