next-tinacms-dos 0.0.0-20240817171706 → 0.0.0-20240822225425

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/dist/handlers.mjs +0 -213
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-tinacms-dos",
3
- "version": "0.0.0-20240817171706",
3
+ "version": "0.0.0-20240822225425",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "files": [
@@ -18,7 +18,7 @@
18
18
  ]
19
19
  },
20
20
  "peerDependencies": {
21
- "tinacms": "0.0.0-20240817171706"
21
+ "tinacms": "0.0.0-20240822225425"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/crypto-js": "^3.1.47",
@@ -28,8 +28,8 @@
28
28
  "react": "^18.3.1",
29
29
  "react-dom": "^18.3.1",
30
30
  "typescript": "4.6.4",
31
- "@tinacms/scripts": "0.0.0-20240817171706",
32
- "tinacms": "0.0.0-20240817171706"
31
+ "@tinacms/scripts": "1.2.0",
32
+ "tinacms": "0.0.0-20240822225425"
33
33
  },
34
34
  "publishConfig": {
35
35
  "registry": "https://registry.npmjs.org"
package/dist/handlers.mjs DELETED
@@ -1,213 +0,0 @@
1
- // src/handlers.ts
2
- import {
3
- S3Client,
4
- ListObjectsCommand,
5
- PutObjectCommand,
6
- DeleteObjectCommand
7
- } from "@aws-sdk/client-s3";
8
- import path from "path";
9
- import fs from "fs";
10
- import multer from "multer";
11
- import { promisify } from "util";
12
- var mediaHandlerConfig = {
13
- api: {
14
- bodyParser: false
15
- }
16
- };
17
- var createMediaHandler = (config, options) => {
18
- const client = new S3Client(config.config);
19
- const bucket = config.bucket;
20
- let mediaRoot = config.mediaRoot || "";
21
- if (mediaRoot) {
22
- if (!mediaRoot.endsWith("/")) {
23
- mediaRoot = mediaRoot + "/";
24
- }
25
- if (mediaRoot.startsWith("/")) {
26
- mediaRoot = mediaRoot.substr(1);
27
- }
28
- }
29
- let cdnUrl = options?.cdnUrl || config.config.endpoint.toString().replace(/http(s|):\/\//i, `https://${bucket}.`);
30
- cdnUrl = cdnUrl + (cdnUrl.endsWith("/") ? "" : "/");
31
- return async (req, res) => {
32
- const isAuthorized = await config.authorized(req, res);
33
- if (!isAuthorized) {
34
- res.status(401).json({ message: "sorry this user is unauthorized" });
35
- return;
36
- }
37
- switch (req.method) {
38
- case "GET":
39
- return listMedia(req, res, client, bucket, mediaRoot, cdnUrl);
40
- case "POST":
41
- return uploadMedia(req, res, client, bucket, mediaRoot, cdnUrl);
42
- case "DELETE":
43
- return deleteAsset(req, res, client, bucket);
44
- default:
45
- res.end(404);
46
- }
47
- };
48
- };
49
- async function uploadMedia(req, res, client, bucket, mediaRoot, cdnUrl) {
50
- const upload = promisify(
51
- multer({
52
- storage: multer.diskStorage({
53
- directory: (req2, file, cb) => {
54
- cb(null, "/tmp");
55
- },
56
- filename: (req2, file, cb) => {
57
- cb(null, file.originalname);
58
- }
59
- })
60
- }).single("file")
61
- );
62
- await upload(req, res);
63
- const { directory } = req.body;
64
- let prefix = directory.replace(/^\//, "").replace(/\/$/, "");
65
- if (prefix)
66
- prefix = prefix + "/";
67
- const filePath = req.file.path;
68
- const blob = fs.readFileSync(filePath);
69
- const filename = path.basename(filePath);
70
- const params = {
71
- Bucket: bucket,
72
- Key: mediaRoot ? path.join(mediaRoot, prefix + filename) : prefix + filename,
73
- Body: blob,
74
- ACL: "public-read"
75
- };
76
- const command = new PutObjectCommand(params);
77
- try {
78
- const src = cdnUrl + prefix + filename;
79
- await client.send(command);
80
- res.json({
81
- type: "file",
82
- id: prefix + filename,
83
- filename,
84
- directory: prefix,
85
- thumbnails: {
86
- "75x75": src,
87
- "400x400": src,
88
- "1000x1000": src
89
- },
90
- src: cdnUrl + (mediaRoot ? path.join(mediaRoot, prefix + filename) : prefix + filename)
91
- });
92
- } catch (e) {
93
- console.error("Error uploading media");
94
- console.error(e);
95
- res.status(500).send(findErrorMessage(e));
96
- }
97
- }
98
- function stripMediaRoot(mediaRoot, key) {
99
- if (!mediaRoot) {
100
- return key;
101
- }
102
- const mediaRootParts = mediaRoot.split("/").filter((part) => part);
103
- if (!mediaRootParts || !mediaRootParts[0]) {
104
- return key;
105
- }
106
- const keyParts = key.split("/").filter((part) => part);
107
- for (let i = 0; i < mediaRootParts.length; i++) {
108
- if (keyParts[0] === mediaRootParts[i]) {
109
- keyParts.shift();
110
- }
111
- }
112
- return keyParts.join("/");
113
- }
114
- async function listMedia(req, res, client, bucket, mediaRoot, cdnUrl) {
115
- try {
116
- const {
117
- directory = "",
118
- limit = 500,
119
- offset
120
- } = req.query;
121
- let prefix = directory.replace(/^\//, "").replace(/\/$/, "");
122
- if (prefix)
123
- prefix = prefix + "/";
124
- const params = {
125
- Bucket: bucket,
126
- Delimiter: "/",
127
- Prefix: mediaRoot ? path.join(mediaRoot, prefix) : prefix,
128
- Marker: offset?.toString(),
129
- MaxKeys: directory && !offset ? +limit + 1 : +limit
130
- };
131
- const response = await client.send(new ListObjectsCommand(params));
132
- const items = [];
133
- response.CommonPrefixes?.forEach(({ Prefix }) => {
134
- const strippedPrefix = stripMediaRoot(mediaRoot, Prefix);
135
- if (!strippedPrefix) {
136
- return;
137
- }
138
- items.push({
139
- id: Prefix,
140
- type: "dir",
141
- filename: path.basename(strippedPrefix),
142
- directory: path.dirname(strippedPrefix)
143
- });
144
- });
145
- items.push(
146
- ...(response.Contents || []).filter((file) => {
147
- const strippedKey = stripMediaRoot(mediaRoot, file.Key);
148
- return strippedKey !== prefix;
149
- }).map(getDOSToTinaFunc(cdnUrl, mediaRoot))
150
- );
151
- res.json({
152
- items,
153
- offset: response.NextMarker
154
- });
155
- } catch (e) {
156
- console.error("Error listing media");
157
- console.error(e);
158
- res.status(500);
159
- const message = findErrorMessage(e);
160
- res.json({ e: message });
161
- }
162
- }
163
- var findErrorMessage = (e) => {
164
- if (typeof e == "string")
165
- return e;
166
- if (e.message)
167
- return e.message;
168
- if (e.error && e.error.message)
169
- return e.error.message;
170
- return "an error occurred";
171
- };
172
- async function deleteAsset(req, res, client, bucket) {
173
- const { media } = req.query;
174
- const [, objectKey] = media;
175
- const params = {
176
- Bucket: bucket,
177
- Key: objectKey
178
- };
179
- try {
180
- const data = await client.send(new DeleteObjectCommand(params));
181
- res.json(data);
182
- } catch (err) {
183
- console.error("Error deleting media");
184
- console.error(err);
185
- res.status(500).json({
186
- message: err.message || "Something went wrong"
187
- });
188
- }
189
- }
190
- function getDOSToTinaFunc(cdnUrl, mediaRoot) {
191
- return function dosToTina(file) {
192
- const strippedKey = stripMediaRoot(mediaRoot, file.Key);
193
- const filename = path.basename(strippedKey);
194
- const directory = path.dirname(strippedKey) + "/";
195
- const src = cdnUrl + file.Key;
196
- return {
197
- id: file.Key,
198
- filename,
199
- directory,
200
- src,
201
- thumbnails: {
202
- "75x75": src,
203
- "400x400": src,
204
- "1000x1000": src
205
- },
206
- type: "file"
207
- };
208
- };
209
- }
210
- export {
211
- createMediaHandler,
212
- mediaHandlerConfig
213
- };