importer-storage 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.
package/dist/index.js ADDED
@@ -0,0 +1,133 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/logic/storage.ts
23
+ import {
24
+ S3Client,
25
+ PutObjectCommand,
26
+ GetObjectTaggingCommand,
27
+ PutObjectTaggingCommand,
28
+ ListObjectsCommand
29
+ } from "@aws-sdk/client-s3";
30
+ import { createReadStream } from "fs";
31
+ import { config } from "dotenv";
32
+ import axios from "axios";
33
+ config();
34
+ if (!process.env.AWS_ACCESS_KEY_ID) {
35
+ throw new Error("'AWS_ACCESS_KEY_ID' not set");
36
+ }
37
+ if (!process.env.AWS_ACCESS_KEY) {
38
+ throw new Error("'AWS_ACCESS_KEY' not set");
39
+ }
40
+ function extractFileInfo(filePath) {
41
+ const parts = filePath.split("/");
42
+ const key = parts[parts.length - 1];
43
+ const extMatch = key.match(/\.([^.]+)$/);
44
+ const extension = extMatch ? extMatch[1].toLowerCase() : null;
45
+ return { key, extension: extension || "jpg" };
46
+ }
47
+ var s3 = new S3Client({
48
+ region: process.env.AWS_REGION,
49
+ credentials: {
50
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID,
51
+ secretAccessKey: process.env.AWS_ACCESS_KEY
52
+ }
53
+ });
54
+ var BUCKET_NAME = process.env.AWS_BUCKET_NAME;
55
+ if (!BUCKET_NAME) {
56
+ throw new Error("'AWS_BUCKET_NAME' name not set");
57
+ }
58
+ function isHttp(url) {
59
+ return url.startsWith("http://") || url.startsWith("https://");
60
+ }
61
+ var S3Storage = class {
62
+ saveFiles(keys, options) {
63
+ return __async(this, null, function* () {
64
+ const urls = [];
65
+ for (const { key, filePath } of keys) {
66
+ const s3Key = `${options.savePath}/${key}`;
67
+ let stream;
68
+ if (isHttp(filePath)) {
69
+ stream = yield axios.get(filePath, {
70
+ responseType: "stream"
71
+ });
72
+ }
73
+ stream = createReadStream(filePath);
74
+ const tagString = options.tags.map(
75
+ (tag) => `${encodeURIComponent(tag.split(":")[0])}=${encodeURIComponent(tag.split(":")[1])}`
76
+ ).join("&");
77
+ const command = new PutObjectCommand({
78
+ Body: stream,
79
+ Bucket: BUCKET_NAME,
80
+ Key: s3Key,
81
+ Tagging: tagString || void 0,
82
+ ContentType: `image/${extractFileInfo(key).extension}`,
83
+ ContentDisposition: "inline"
84
+ });
85
+ yield s3.send(command);
86
+ urls.push(
87
+ `https://${BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${s3Key}`
88
+ );
89
+ }
90
+ return { urls: urls.join(",") };
91
+ });
92
+ }
93
+ removeTag(savePath, tags) {
94
+ return __async(this, null, function* () {
95
+ var _a;
96
+ const list = yield s3.send(
97
+ new ListObjectsCommand({
98
+ Bucket: BUCKET_NAME,
99
+ Prefix: savePath + "/",
100
+ MaxKeys: 1e3
101
+ })
102
+ );
103
+ if (!list.Contents) return;
104
+ for (const obj of list.Contents) {
105
+ if (!obj.Key) continue;
106
+ const currentTags = yield s3.send(
107
+ new GetObjectTaggingCommand({
108
+ Bucket: BUCKET_NAME,
109
+ Key: obj.Key
110
+ })
111
+ );
112
+ const filteredTags = ((_a = currentTags.TagSet) == null ? void 0 : _a.filter(
113
+ (t) => !tags.includes(`${t.Key}:${t.Value}`)
114
+ )) || [];
115
+ yield s3.send(
116
+ new PutObjectTaggingCommand({
117
+ Bucket: BUCKET_NAME,
118
+ Key: obj.Key,
119
+ Tagging: { TagSet: filteredTags }
120
+ })
121
+ );
122
+ }
123
+ });
124
+ }
125
+ };
126
+
127
+ // src/index.ts
128
+ var Storage = {
129
+ S3Storage
130
+ };
131
+ export {
132
+ Storage
133
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "importer-storage",
3
+ "private": false,
4
+ "version": "1.0.1",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist",
8
+ "README.md"
9
+ ],
10
+ "type": "module",
11
+ "scripts": {
12
+ "build": "tsup --format cjs,esm,iife --global-name CalculatorModule --target es2015",
13
+ "lint": "eslint .",
14
+ "prettier": "prettier --check .",
15
+ "prettier:fix": "prettier --write .",
16
+ "test": "vitest",
17
+ "upload:test": "npx tsx scripts/upload.ts",
18
+ "remove-tag": "npx tsx scripts/remove-tag.ts"
19
+ },
20
+ "dependencies": {
21
+ "@aws-sdk/client-s3": "^3.826.0",
22
+ "axios": "^1.9.0",
23
+ "dotenv": "^16.5.0",
24
+ "globals": "^16.2.0"
25
+ },
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.19.0",
28
+ "@types/node": "^24.0.0",
29
+ "eslint": "^9.19.0",
30
+ "prettier": "^3.4.2",
31
+ "tsup": "^8.3.6",
32
+ "typescript": "~5.8.3",
33
+ "typescript-eslint": "^8.34.0",
34
+ "vitest": "^3.0.5"
35
+ }
36
+ }