Skip to content

Commit 7eda75a

Browse files
authored
Merge pull request #5 from fixpoint/support-mimetype
Support mimetype
2 parents 5f8df43 + d128a2c commit 7eda75a

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

.github/workflows/test.yml

+4
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,31 @@ jobs:
3131
uses: ./
3232
with:
3333
connection-string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
34+
container: 'azblob-upload-artifact'
3435
name: '${{ matrix.os }}/azblob-upload-artifact1'
3536
path: 'README.md'
3637

3738
- name: Upload with path (file)
3839
uses: ./
3940
with:
4041
connection-string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
42+
container: 'azblob-upload-artifact'
4143
name: '${{ matrix.os }}/azblob-upload-artifact2'
4244
path: 'path/to/artifact1/hello.txt'
4345

4446
- name: Upload with path (directory)
4547
uses: ./
4648
with:
4749
connection-string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
50+
container: 'azblob-upload-artifact'
4851
name: '${{ matrix.os }}/azblob-upload-artifact3'
4952
path: 'path/to/artifact2'
5053

5154
- name: Upload with path (directory, overwrite)
5255
uses: ./
5356
with:
5457
connection-string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
58+
container: 'azblob-upload-artifact'
5559
name: '${{ matrix.os }}/azblob-upload-artifact3'
5660
path: 'path/to/artifact1'
5761
cleanup: 'true'

dist/index.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -19978,16 +19978,17 @@ function serial(list, iterator, callback)
1997819978

1997919979
Object.defineProperty(exports, "__esModule", { value: true });
1998019980
const tslib_1 = __webpack_require__(422);
19981+
const mime = tslib_1.__importStar(__webpack_require__(779));
1998119982
const storage_blob_1 = __webpack_require__(9);
1998219983
const core = tslib_1.__importStar(__webpack_require__(470));
1998319984
const fs_1 = __webpack_require__(747);
1998419985
const util_1 = __webpack_require__(648);
1998519986
const path_1 = __webpack_require__(622);
1998619987
async function upload(connectionString, name, path, container, cleanup) {
19987-
const serviceClient = await storage_blob_1.BlobServiceClient.fromConnectionString(connectionString);
19988+
const serviceClient = storage_blob_1.BlobServiceClient.fromConnectionString(connectionString);
1998819989
// Create container if necessary
1998919990
core.info(`Creating a container "${container}" ...`);
19990-
const containerClient = await serviceClient.getContainerClient(container);
19991+
const containerClient = serviceClient.getContainerClient(container);
1999119992
if (!(await containerClient.exists())) {
1999219993
await containerClient.create();
1999319994
}
@@ -19997,25 +19998,35 @@ async function upload(connectionString, name, path, container, cleanup) {
1999719998
if (!blob.name.startsWith(name + '/')) {
1999819999
continue;
1999920000
}
20000-
const blockClient = await containerClient.getBlockBlobClient(blob.name);
20001+
const blockClient = containerClient.getBlockBlobClient(blob.name);
2000120002
await blockClient.delete();
2000220003
}
2000320004
}
20005+
const uploadFileWithContentType = async (src, dst) => {
20006+
const mt = mime.lookup(src);
20007+
const blobHTTPHeaders = mt
20008+
? {
20009+
blobContentType: mt,
20010+
}
20011+
: {};
20012+
const blockClient = containerClient.getBlockBlobClient(dst);
20013+
await blockClient.uploadFile(src, {
20014+
blobHTTPHeaders,
20015+
});
20016+
};
2000420017
// Upload the file/directory
2000520018
const stat = await fs_1.promises.lstat(path);
2000620019
if (stat.isDirectory()) {
2000720020
for (const src of await util_1.walk(path)) {
2000820021
const dst = [name, path_1.relative(path, src).replace(/\\/g, '/')].join('/');
2000920022
core.info(`Uploading ${src} to ${dst} ...`);
20010-
const blockClient = await containerClient.getBlockBlobClient(dst);
20011-
await blockClient.uploadFile(src);
20023+
await uploadFileWithContentType(src, dst);
2001220024
}
2001320025
}
2001420026
else {
2001520027
const dst = [name, path_1.basename(path)].join('/');
2001620028
core.info(`Uploading ${path} to ${dst} ...`);
20017-
const blockClient = await containerClient.getBlockBlobClient(dst);
20018-
await blockClient.uploadFile(path);
20029+
await uploadFileWithContentType(path, dst);
2001920030
}
2002020031
}
2002120032
exports.upload = upload;

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
"dependencies": {
3333
"@actions/core": "^1.2.2",
3434
"@actions/exec": "^1.0.3",
35-
"@azure/storage-blob": "^12.1.0"
35+
"@azure/storage-blob": "^12.1.0",
36+
"mime-types": "^2.1.26"
3637
},
3738
"devDependencies": {
39+
"@types/mime-types": "^2.1.0",
3840
"@types/node": "^13.7.1",
3941
"@typescript-eslint/eslint-plugin": "^2.20.0",
4042
"@typescript-eslint/parser": "^2.20.0",

src/upload.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as mime from 'mime-types';
12
import { BlobServiceClient } from '@azure/storage-blob';
23
import * as core from '@actions/core';
34
import { promises as fs } from 'fs';
@@ -11,13 +12,13 @@ export async function upload(
1112
container: string,
1213
cleanup: boolean,
1314
) {
14-
const serviceClient = await BlobServiceClient.fromConnectionString(
15+
const serviceClient = BlobServiceClient.fromConnectionString(
1516
connectionString,
1617
);
1718

1819
// Create container if necessary
1920
core.info(`Creating a container "${container}" ...`);
20-
const containerClient = await serviceClient.getContainerClient(container);
21+
const containerClient = serviceClient.getContainerClient(container);
2122
if (!(await containerClient.exists())) {
2223
await containerClient.create();
2324
}
@@ -28,24 +29,35 @@ export async function upload(
2829
if (!blob.name.startsWith(name + '/')) {
2930
continue;
3031
}
31-
const blockClient = await containerClient.getBlockBlobClient(blob.name);
32+
const blockClient = containerClient.getBlockBlobClient(blob.name);
3233
await blockClient.delete();
3334
}
3435
}
3536

37+
const uploadFileWithContentType = async (src: string, dst: string) => {
38+
const mt = mime.lookup(src);
39+
const blobHTTPHeaders = mt
40+
? {
41+
blobContentType: mt,
42+
}
43+
: {};
44+
const blockClient = containerClient.getBlockBlobClient(dst);
45+
await blockClient.uploadFile(src, {
46+
blobHTTPHeaders,
47+
});
48+
};
49+
3650
// Upload the file/directory
3751
const stat = await fs.lstat(path);
3852
if (stat.isDirectory()) {
3953
for (const src of await walk(path)) {
4054
const dst = [name, relative(path, src).replace(/\\/g, '/')].join('/');
4155
core.info(`Uploading ${src} to ${dst} ...`);
42-
const blockClient = await containerClient.getBlockBlobClient(dst);
43-
await blockClient.uploadFile(src);
56+
await uploadFileWithContentType(src, dst);
4457
}
4558
} else {
4659
const dst = [name, basename(path)].join('/');
4760
core.info(`Uploading ${path} to ${dst} ...`);
48-
const blockClient = await containerClient.getBlockBlobClient(dst);
49-
await blockClient.uploadFile(path);
61+
await uploadFileWithContentType(path, dst);
5062
}
5163
}

0 commit comments

Comments
 (0)