Skip to content

Commit

Permalink
feat: download from Encore in OSC (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored Jul 2, 2024
1 parent ccd7049 commit 04fa504
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface PackageOptions {
stagingDir?: string;
noImplicitAudio?: boolean;
shakaExecutable?: string;
serviceAccessToken?: string;
}

export async function doPackage(opts: PackageOptions) {
Expand All @@ -42,7 +43,8 @@ export async function prepare(
export async function download(
input: Input,
source?: URL,
stagingDir?: string
stagingDir?: string,
serviceAccessToken?: string
): Promise<string> {
if (!source) {
return input.filename;
Expand Down Expand Up @@ -71,6 +73,30 @@ export async function download(
}
console.log(`Downloaded ${input.filename} to ${localFilename}`);
return localFilename;
} else if (source.protocol === 'http:' || source.protocol === 'https:') {
const localFilename = join(stagingDir, path.basename(input.filename));
const auth: string[] = [];
if (serviceAccessToken) {
auth.push('-H');
auth.push(`x-jwt: Bearer ${serviceAccessToken}`);
}
const { status, stdout, stderr } = spawnSync(
'curl',
auth.concat([
'-v',
'-o',
localFilename,
source.href.replace(/\/$/, '') + input.filename
])
);
if (stderr) {
console.log(stderr.toString());
}
if (status !== 0) {
throw new Error('Download failed');
}
console.log(`Downloaded ${input.filename} to ${localFilename}`);
return localFilename;
} else {
throw new Error(`Unsupported protocol for download: ${source.protocol}`);
}
Expand Down Expand Up @@ -114,11 +140,17 @@ export async function uploadPackage(dest: URL, stagingDir: string) {
}

export async function createPackage(opts: PackageOptions) {
const { inputs, source, stagingDir, noImplicitAudio } = opts;
const { inputs, source, stagingDir, noImplicitAudio, serviceAccessToken } =
opts;
const sourceUrl = toUrlOrUndefined(source);
const downloadedInputs: Input[] = await Promise.all(
inputs.map(async (input) => {
const filename = await download(input, sourceUrl, stagingDir);
const filename = await download(
input,
sourceUrl,
stagingDir,
serviceAccessToken
);
return {
...input,
filename
Expand Down

0 comments on commit 04fa504

Please sign in to comment.