Skip to content

Commit 6506324

Browse files
committed
better parsing for input file params
1 parent bd14bec commit 6506324

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
description: The ID of the release to which to upload files.
66
required: false
77
files:
8-
description: Paths to the files to upload. Separated by newline. You can specify the MIME type for each file after the file path, preceded by a `&`.
8+
description: Paths to the files to upload. Separated by newline.
99
required: true
1010
runs:
1111
using: docker

src/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ function error(message: string): never {
1111
process.exit(1);
1212
}
1313

14+
function parseInputFileParams(input: string): {path: string, params: Record<string, string>} {
15+
const [filePath, ...rawParams] = input.split(/(?<!\\);\s*/);
16+
const params: Record<string, string> = {};
17+
for (const rawParam of rawParams) {
18+
const equalsIndex = rawParam.indexOf("=");
19+
if (equalsIndex === -1) params[rawParam.trim()] = "";
20+
else params[rawParam.substring(0, equalsIndex)] = rawParam.substring(equalsIndex + 1);
21+
}
22+
return {path: filePath!, params};
23+
}
24+
1425
// Action inputs
1526
const GH_INPUTS: Record<string, string> = JSON.parse(process.env.GH_INPUTS!);
1627
const GITHUB_TOKEN = process.env.GITHUB_TOKEN!;
@@ -33,11 +44,12 @@ else {
3344
// Read the files
3445
core.info("Reading files...");
3546
const files = await Promise.all(inputs.files.split("\n").map(async f => {
36-
const hasMimeType = f.lastIndexOf("&") !== -1;
37-
const filePath = (hasMimeType ? f.substring(0, f.lastIndexOf("&")) : f).trim();
38-
const mimeType = (hasMimeType ? f.substring(f.lastIndexOf("&") + 1) : "application/octet-stream").trim().toLowerCase();
47+
const {path: filePath, params} = parseInputFileParams(f);
48+
const mimeType = params["type"] ?? "application/octet-stream";
49+
const fileName = params["filename"] ?? path.basename(filePath);
50+
3951
const data = await fs.readFile(filePath);
40-
core.info(`Read file ${filePath} (type ${mimeType}, size ${data.length} bytes)`);
52+
core.info(`Read file: ${filePath} (type=${mimeType}; name=${fileName}; size=${data.length})`);
4153
return new File([data], path.basename(filePath), {type: mimeType});
4254
}));
4355

0 commit comments

Comments
 (0)