Skip to content

Commit

Permalink
Export Compression support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielPXL committed Mar 13, 2024
1 parent e36ecd3 commit e3cfd2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ handlers.set("callResponse", (data, call) => {
});

handlers.set("setStream", (data, call) => {
const stream = new ReadableStream({
let stream = new ReadableStream({
async start(controller) {
const { queue, shouldClose } = await call("streamStart", null);
for (const buf of queue) {
Expand All @@ -127,6 +127,10 @@ handlers.set("setStream", (data, call) => {
}
});

if (data.compress) {
stream = stream.pipeThrough(new CompressionStream("gzip"));
}

function randomString(n: number) {
let str = "";
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Expand Down
9 changes: 8 additions & 1 deletion src/export/ExportDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const commonControlsSchema: ControlSectionEntry[] = [
min: 1,
max: 600,
default: 60
},
{
type: "checkbox",
text: "Compress (as .gz)",
id: "compress",
default: true
}
]

Expand Down Expand Up @@ -131,9 +137,10 @@ async function continueExport() {
const seqName = await AudioWorkerComms.call("getCurrentSeqSymbol");
const sampleRate = commonControls.get("sampleRate");
const seconds = commonControls.get("seconds");
const compress = commonControls.get("compress");
const configSection = exportControls[exporterIndex];

ProgressStatus.reset(seconds);

await prepareStreamExport(exporterIndex, sampleRate, seconds, seqName, configSection);
await prepareStreamExport(exporterIndex, sampleRate, seconds, compress, seqName, configSection);
}
14 changes: 10 additions & 4 deletions src/export/ExportManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function init() {
// Maybe check if it's available and use it instead.
// https://web.dev/file-system-access/

export async function prepareStreamExport(exporterIndex: number, sampleRate: number, seconds: number, seqName: string, configSection: ControlSection | null) {
export async function prepareStreamExport(exporterIndex: number, sampleRate: number, seconds: number, compress: boolean, seqName: string, configSection: ControlSection | null) {
const stream = exporters[exporterIndex].getStream(sampleRate, seconds, ProgressStatus.update, configSection);
await ServiceWorkerComms.ready;

Expand Down Expand Up @@ -96,11 +96,17 @@ export async function prepareStreamExport(exporterIndex: number, sampleRate: num
});
});

let filename = `${seqName}.${exporters[exporterIndex].fileExtension}`;
if (compress) {
filename += ".gz";
}

ServiceWorkerComms.send("setStream", {
filename: `${seqName}.${exporters[exporterIndex].fileExtension}`,
filename,
compress,
headers: {
"Content-Type": exporters[exporterIndex].mimeType,
"Content-Disposition": `attachment; filename="${seqName}.${exporters[exporterIndex].fileExtension}"`
"Content-Type": compress ? "application/gzip" : exporters[exporterIndex].mimeType,
"Content-Disposition": `attachment; filename="${filename}"`
}
});
}

0 comments on commit e3cfd2c

Please sign in to comment.