Skip to content

Commit

Permalink
Sanity check content mimes
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed May 2, 2024
1 parent bf91123 commit 8cf2cb4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
10 changes: 9 additions & 1 deletion bin/download-flash-cn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function main() {
}));

const each = async resource => {
const {name, source, referer, file} = resource.info;
const {name, source, referer, file, mimetype} = resource.info;
const filedir = pathJoin(outdir, name);
const filepath = pathJoin(filedir, file);

Expand All @@ -52,6 +52,14 @@ async function main() {
'User-Agent': userAgent,
Referer: referer
},
response(response) {
const ct = response.headers.get('content-type');
if (ct !== mimetype) {
throw new Error(
`Mimetype: ${ct} != ${mimetype}: ${source}`
);
}
},
progress({size, total}) {
resource.download = size / total;
}
Expand Down
10 changes: 9 additions & 1 deletion bin/download-harman-air-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function main() {
}));

const each = async resource => {
const {name, source, file} = resource.info;
const {name, source, file, mimetype} = resource.info;
const filedir = pathJoin(outdir, name);
const filepath = pathJoin(filedir, file);

Expand All @@ -44,6 +44,14 @@ async function main() {
headers: {
'User-Agent': userAgent
},
response(response) {
const ct = response.headers.get('content-type');
if (ct !== mimetype) {
throw new Error(
`Mimetype: ${ct} != ${mimetype}: ${source}`
);
}
},
progress({size, total}) {
resource.download = size / total;
}
Expand Down
10 changes: 9 additions & 1 deletion bin/download-harman-air-sdk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function main() {
}));

const each = async resource => {
const {name, source, file} = resource.info;
const {name, source, file, mimetype} = resource.info;
const filedir = pathJoin(outdir, name);
const filepath = pathJoin(filedir, file);

Expand All @@ -48,6 +48,14 @@ async function main() {
'User-Agent': userAgent,
Cookie: cookieHeader
},
response(response) {
const ct = response.headers.get('content-type');
if (ct !== mimetype) {
throw new Error(
`Mimetype: ${ct} != ${mimetype}: ${source}`
);
}
},
progress({size, total}) {
resource.download = size / total;
}
Expand Down
3 changes: 3 additions & 0 deletions util/download.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export async function download(output, url, opts = {}) {
const response = await retry(() =>
fetch(url, {headers: opts.headers || {}})
);
if (opts.response) {
opts.response(response);
}
if (response.status !== 200) {
throw new Error(`Status code: ${response.status}: ${url}`);
}
Expand Down
28 changes: 25 additions & 3 deletions util/flashcn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ function dateNorm(date) {
return date;
}

function mimetype(file) {
if (/\.exe/i.test(file)) {
return 'application/x-msdownload';
}
if (/\.dmg/i.test(file)) {
return 'application/x-apple-diskimage';
}
if (/\.rpm/i.test(file)) {
return 'application/octet-stream';
}
if (/\.tar\.gz/i.test(file)) {
return 'application/octet-stream';
}
if (/\.swc/i.test(file)) {
return 'application/octet-stream';
}
return null;
}

async function listRelease() {
const htmlUrl = 'https://www.flash.cn/download';
const fverUrl = 'https://api.flash.cn/config/flashVersion';
Expand Down Expand Up @@ -132,21 +151,23 @@ async function listRelease() {
// }

const source = getSource(info.downloadURL, info.version);
const file = urlFile(source);
const type = idRelease.get(id);
if (!type) {
throw new Error(`Unknown id: ${id}`);
}

r.push({
name: `flash-player-${info.version}-${type}-cn`,
file: urlFile(source),
file,
source,
referer: htmlUrl,
list: 'release',
id,
date: dateNorm(info.date),
version: info.version,
size: info.size
size: info.size,
mimetype: mimetype(file)
});
ids.add(id);
}
Expand Down Expand Up @@ -229,7 +250,8 @@ async function listDebug() {
id,
date: dated,
version,
size: null
size: null,
mimetype: mimetype(file)
});
ids.add(id);
}
Expand Down
12 changes: 7 additions & 5 deletions util/harman.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const userAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0';

const runtimeFiles = [
['AdobeAIR.exe', 'windows'],
['AdobeAIR.dmg', 'mac']
['AdobeAIR.exe', 'windows', 'application/x-msdownload'],
['AdobeAIR.dmg', 'mac', 'application/x-apple-diskimage']
];

const legacy = new Set([
Expand Down Expand Up @@ -101,7 +101,8 @@ export async function sdks() {
name,
version,
file,
source
source,
mimetype: 'application/octet-stream'
});
}

Expand Down Expand Up @@ -188,7 +189,7 @@ export async function runtimes() {
}

const downloads = [];
for (const [file, os] of runtimeFiles) {
for (const [file, os, mimetype] of runtimeFiles) {
const sha256 = sha256s.get(file);
if (!sha256) {
throw new Error(`No sha256 for: ${file}`);
Expand All @@ -199,7 +200,8 @@ export async function runtimes() {
version,
file,
sha256,
source: `${runtimeFileBase}${file}`
source: `${runtimeFileBase}${file}`,
mimetype
});
}
return downloads;
Expand Down

0 comments on commit 8cf2cb4

Please sign in to comment.