kap-r2 1.0.1

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.
@@ -0,0 +1,56 @@
1
+ interface KapConfig {
2
+ get(key: string): string;
3
+ }
4
+ interface KapContext {
5
+ filePath(): Promise<string>;
6
+ config: KapConfig;
7
+ setProgress(text: string, percentage?: number): void;
8
+ copyToClipboard(text: string): void;
9
+ notify(text: string, action?: () => void): void;
10
+ cancel(): void;
11
+ }
12
+ export declare const shareServices: {
13
+ title: string;
14
+ formats: string[];
15
+ action: (context: KapContext) => Promise<void>;
16
+ configDescription: string;
17
+ config: {
18
+ accountId: {
19
+ title: string;
20
+ type: string;
21
+ default: string;
22
+ required: boolean;
23
+ };
24
+ accessKeyId: {
25
+ title: string;
26
+ type: string;
27
+ default: string;
28
+ required: boolean;
29
+ };
30
+ secretAccessKey: {
31
+ title: string;
32
+ type: string;
33
+ default: string;
34
+ required: boolean;
35
+ };
36
+ bucket: {
37
+ title: string;
38
+ type: string;
39
+ default: string;
40
+ required: boolean;
41
+ };
42
+ directory: {
43
+ title: string;
44
+ type: string;
45
+ default: string;
46
+ required: boolean;
47
+ };
48
+ publicUrl: {
49
+ title: string;
50
+ type: string;
51
+ default: string;
52
+ required: boolean;
53
+ };
54
+ };
55
+ }[];
56
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,142 @@
1
+ import { createReadStream } from "node:fs";
2
+ import { stat } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { S3Client } from "@aws-sdk/client-s3";
5
+ import { Upload } from "@aws-sdk/lib-storage";
6
+ const contentTypes = new Map([
7
+ [".gif", "image/gif"],
8
+ [".mp4", "video/mp4"],
9
+ [".webm", "video/webm"],
10
+ [".apng", "image/apng"],
11
+ ]);
12
+ const isValidAccountId = (id) => {
13
+ return /^[a-f0-9]{32}$/.test(id);
14
+ };
15
+ const isValidBucketName = (name) => {
16
+ return /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/.test(name);
17
+ };
18
+ const action = async (context) => {
19
+ const accountId = context.config.get("accountId");
20
+ const accessKeyId = context.config.get("accessKeyId");
21
+ const secretAccessKey = context.config.get("secretAccessKey");
22
+ const bucket = context.config.get("bucket");
23
+ const directory = context.config.get("directory") || "";
24
+ const publicUrl = context.config.get("publicUrl");
25
+ if (!isValidAccountId(accountId)) {
26
+ context.notify("Invalid Account ID. It should be a 32-character hex string.");
27
+ context.cancel();
28
+ return;
29
+ }
30
+ if (!isValidBucketName(bucket)) {
31
+ context.notify("Invalid Bucket Name. Use lowercase letters, numbers, and hyphens.");
32
+ context.cancel();
33
+ return;
34
+ }
35
+ if (!publicUrl.startsWith("https://")) {
36
+ context.notify("Public URL must start with https://");
37
+ context.cancel();
38
+ return;
39
+ }
40
+ const filePath = await context.filePath();
41
+ const fileStats = await stat(filePath);
42
+ context.setProgress("Uploading to R2…", 0);
43
+ const client = new S3Client({
44
+ region: "auto",
45
+ endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
46
+ credentials: {
47
+ accessKeyId,
48
+ secretAccessKey,
49
+ },
50
+ });
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
+ try {
56
+ const upload = new Upload({
57
+ client,
58
+ params: {
59
+ Bucket: bucket,
60
+ Key: key,
61
+ Body: createReadStream(filePath),
62
+ ContentType: contentType,
63
+ },
64
+ queueSize: 4,
65
+ partSize: 1024 * 1024 * 5,
66
+ leavePartsOnError: false,
67
+ });
68
+ upload.on("httpUploadProgress", (progress) => {
69
+ if (progress.loaded && fileStats.size) {
70
+ const percentage = progress.loaded / fileStats.size;
71
+ context.setProgress("Uploading to R2…", percentage);
72
+ }
73
+ });
74
+ await upload.done();
75
+ const baseUrl = publicUrl.replace(/\/$/, "");
76
+ const encodedKey = key.split("/").map(encodeURIComponent).join("/");
77
+ const uploadUrl = `${baseUrl}/${encodedKey}`;
78
+ context.copyToClipboard(uploadUrl);
79
+ context.notify("R2 URL copied to the clipboard");
80
+ }
81
+ catch (error) {
82
+ const message = error instanceof Error ? error.message : "Unknown error";
83
+ if (message.includes("InvalidAccessKeyId") || message.includes("SignatureDoesNotMatch")) {
84
+ context.notify("Authentication failed. Check your Access Key ID and Secret.");
85
+ }
86
+ else if (message.includes("NoSuchBucket")) {
87
+ context.notify("Bucket not found. Check your bucket name.");
88
+ }
89
+ else if (message.includes("AccessDenied")) {
90
+ context.notify("Access denied. Check your API token permissions.");
91
+ }
92
+ else {
93
+ context.notify(`Upload failed: ${message}`);
94
+ }
95
+ context.cancel();
96
+ }
97
+ };
98
+ const r2 = {
99
+ title: "Share to R2",
100
+ formats: ["gif", "mp4", "webm", "apng"],
101
+ action,
102
+ configDescription: "Configure your Cloudflare R2 credentials. Get your API tokens from the Cloudflare dashboard under R2 > Manage R2 API Tokens.",
103
+ config: {
104
+ accountId: {
105
+ title: "Account ID",
106
+ type: "string",
107
+ default: "",
108
+ required: true,
109
+ },
110
+ accessKeyId: {
111
+ title: "Access Key ID",
112
+ type: "string",
113
+ default: "",
114
+ required: true,
115
+ },
116
+ secretAccessKey: {
117
+ title: "Secret Access Key",
118
+ type: "string",
119
+ default: "",
120
+ required: true,
121
+ },
122
+ bucket: {
123
+ title: "Bucket Name",
124
+ type: "string",
125
+ default: "",
126
+ required: true,
127
+ },
128
+ directory: {
129
+ title: "Directory",
130
+ type: "string",
131
+ default: "",
132
+ required: false,
133
+ },
134
+ publicUrl: {
135
+ title: "Public URL",
136
+ type: "string",
137
+ default: "",
138
+ required: true,
139
+ },
140
+ },
141
+ };
142
+ export const shareServices = [r2];
package/license ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Tristan Remy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "kap-r2",
3
+ "version": "1.0.1",
4
+ "description": "Share on Cloudflare R2",
5
+ "license": "MIT",
6
+ "repository": "tristanremy/kap-r2",
7
+ "author": {
8
+ "name": "Tristan Remy",
9
+ "url": "https://github.com/tristanremy"
10
+ },
11
+ "type": "module",
12
+ "exports": "./dist/index.js",
13
+ "main": "./dist/index.js",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "keywords": [
18
+ "kap-plugin",
19
+ "cloudflare",
20
+ "r2",
21
+ "upload",
22
+ "share",
23
+ "storage"
24
+ ],
25
+ "kap": {
26
+ "version": ">=3.0.0"
27
+ },
28
+ "dependencies": {
29
+ "@aws-sdk/client-s3": "^3.728.0",
30
+ "@aws-sdk/lib-storage": "^3.728.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^20.0.0",
34
+ "typescript": "^5.0.0"
35
+ },
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc"
41
+ }
42
+ }
package/readme.md ADDED
@@ -0,0 +1,38 @@
1
+ # kap-r2
2
+
3
+ > [Kap](https://github.com/wulkano/Kap) plugin - Share on Cloudflare R2
4
+
5
+ ## Install
6
+
7
+ In the `Kap` menu, go to `Preferences…`, select the `Plugins` pane, find this plugin, and click `Install`.
8
+
9
+ ## Configuration
10
+
11
+ | Setting | Description |
12
+ |---------|-------------|
13
+ | **Account ID** | Your Cloudflare account ID |
14
+ | **Access Key ID** | R2 API token access key |
15
+ | **Secret Access Key** | R2 API token secret |
16
+ | **Bucket Name** | Name of your R2 bucket |
17
+ | **Directory** | Optional subfolder path (e.g., `recordings/`) |
18
+ | **Public URL** | Your bucket's public URL (R2.dev or custom domain) |
19
+
20
+ ### Getting R2 API Credentials
21
+
22
+ 1. Go to [Cloudflare Dashboard](https://dash.cloudflare.com)
23
+ 2. Navigate to **R2** > **Manage R2 API Tokens**
24
+ 3. Click **Create API Token**
25
+ 4. Grant **Object Read & Write** permissions
26
+ 5. Save the **Access Key ID** and **Secret Access Key**
27
+
28
+ ### Setting Up Public Access
29
+
30
+ Enable the R2.dev subdomain in bucket settings or connect a custom domain.
31
+
32
+ ## Usage
33
+
34
+ After recording in Kap, select an export format, then click **Share to R2**.
35
+
36
+ ## License
37
+
38
+ MIT © [Tristan Remy](https://github.com/tristanremy)