kap-r2 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +57 -36
- package/package.json +4 -6
package/dist/index.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.shareServices = void 0;
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const promises_1 = require("node:fs/promises");
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const node_https_1 = __importDefault(require("node:https"));
|
|
11
|
+
const aws4_1 = __importDefault(require("aws4"));
|
|
6
12
|
const contentTypes = new Map([
|
|
7
13
|
[".gif", "image/gif"],
|
|
8
14
|
[".mp4", "video/mp4"],
|
|
@@ -38,42 +44,57 @@ const action = async (context) => {
|
|
|
38
44
|
return;
|
|
39
45
|
}
|
|
40
46
|
const filePath = await context.filePath();
|
|
41
|
-
const fileStats = await stat(filePath);
|
|
47
|
+
const fileStats = await (0, promises_1.stat)(filePath);
|
|
48
|
+
const fileBuffer = await (0, promises_1.readFile)(filePath);
|
|
42
49
|
context.setProgress("Uploading to R2…", 0);
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
const filename = node_path_1.default.basename(filePath);
|
|
51
|
+
const key = directory ? node_path_1.default.posix.join(directory, filename) : filename;
|
|
52
|
+
const extension = node_path_1.default.extname(filename);
|
|
53
|
+
const contentType = contentTypes.get(extension) || "application/octet-stream";
|
|
54
|
+
const host = `${bucket}.${accountId}.r2.cloudflarestorage.com`;
|
|
55
|
+
const encodedKey = key.split("/").map(encodeURIComponent).join("/");
|
|
56
|
+
const request = aws4_1.default.sign({
|
|
57
|
+
host,
|
|
58
|
+
path: `/${encodedKey}`,
|
|
59
|
+
method: "PUT",
|
|
60
|
+
headers: {
|
|
61
|
+
"Content-Type": contentType,
|
|
62
|
+
"Content-Length": fileStats.size,
|
|
49
63
|
},
|
|
64
|
+
body: fileBuffer,
|
|
65
|
+
service: "s3",
|
|
66
|
+
region: "auto",
|
|
67
|
+
}, {
|
|
68
|
+
accessKeyId,
|
|
69
|
+
secretAccessKey,
|
|
50
70
|
});
|
|
51
|
-
const filename = path.basename(filePath);
|
|
52
|
-
const key = directory ? path.posix.join(directory, filename) : filename;
|
|
53
|
-
const extension = path.extname(filename);
|
|
54
|
-
const contentType = contentTypes.get(extension) || "application/octet-stream";
|
|
55
71
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
await new Promise((resolve, reject) => {
|
|
73
|
+
const req = node_https_1.default.request({
|
|
74
|
+
hostname: host,
|
|
75
|
+
path: `/${encodedKey}`,
|
|
76
|
+
method: "PUT",
|
|
77
|
+
headers: request.headers,
|
|
78
|
+
}, (res) => {
|
|
79
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
80
|
+
resolve();
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const chunks = [];
|
|
84
|
+
res.on("data", (chunk) => chunks.push(chunk));
|
|
85
|
+
res.on("end", () => reject(new Error(`HTTP ${res.statusCode}: ${Buffer.concat(chunks).toString()}`)));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
req.on("error", reject);
|
|
89
|
+
const stream = (0, node_fs_1.createReadStream)(filePath);
|
|
90
|
+
let uploaded = 0;
|
|
91
|
+
stream.on("data", (chunk) => {
|
|
92
|
+
uploaded += Buffer.byteLength(chunk);
|
|
93
|
+
context.setProgress("Uploading to R2…", uploaded / fileStats.size);
|
|
94
|
+
});
|
|
95
|
+
stream.pipe(req);
|
|
73
96
|
});
|
|
74
|
-
await upload.done();
|
|
75
97
|
const baseUrl = publicUrl.replace(/\/$/, "");
|
|
76
|
-
const encodedKey = key.split("/").map(encodeURIComponent).join("/");
|
|
77
98
|
const uploadUrl = `${baseUrl}/${encodedKey}`;
|
|
78
99
|
context.copyToClipboard(uploadUrl);
|
|
79
100
|
context.notify("R2 URL copied to the clipboard");
|
|
@@ -139,4 +160,4 @@ const r2 = {
|
|
|
139
160
|
},
|
|
140
161
|
},
|
|
141
162
|
};
|
|
142
|
-
|
|
163
|
+
exports.shareServices = [r2];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kap-r2",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Share on Cloudflare R2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tristanremy/kap-r2",
|
|
@@ -8,8 +8,6 @@
|
|
|
8
8
|
"name": "Tristan Remy",
|
|
9
9
|
"url": "https://github.com/tristanremy"
|
|
10
10
|
},
|
|
11
|
-
"type": "module",
|
|
12
|
-
"exports": "./dist/index.js",
|
|
13
11
|
"main": "./dist/index.js",
|
|
14
12
|
"files": [
|
|
15
13
|
"dist"
|
|
@@ -26,15 +24,15 @@
|
|
|
26
24
|
"version": ">=3.0.0"
|
|
27
25
|
},
|
|
28
26
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"@aws-sdk/lib-storage": "^3.728.0"
|
|
27
|
+
"aws4": "^1.13.2"
|
|
31
28
|
},
|
|
32
29
|
"devDependencies": {
|
|
30
|
+
"@types/aws4": "^1.11.6",
|
|
33
31
|
"@types/node": "^20.0.0",
|
|
34
32
|
"typescript": "^5.0.0"
|
|
35
33
|
},
|
|
36
34
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
35
|
+
"node": ">=14"
|
|
38
36
|
},
|
|
39
37
|
"scripts": {
|
|
40
38
|
"build": "tsc"
|