@teamkeel/functions-runtime 0.390.0-next.5 → 0.390.0-next.7
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/package.json +1 -1
- package/src/InlineFile.js +58 -13
package/package.json
CHANGED
package/src/InlineFile.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const {
|
|
2
|
+
S3Client,
|
|
3
|
+
PutObjectCommand,
|
|
4
|
+
GetObjectCommand,
|
|
5
|
+
} = require("@aws-sdk/client-s3");
|
|
2
6
|
const { fromEnv } = require("@aws-sdk/credential-providers");
|
|
3
7
|
const { useDatabase } = require("./database");
|
|
4
8
|
const { DatabaseError } = require("./errors");
|
|
5
9
|
const KSUID = require("ksuid");
|
|
6
10
|
|
|
7
11
|
class InlineFile {
|
|
8
|
-
constructor(filename, contentType, size, url) {
|
|
12
|
+
constructor(filename, contentType, size, url, key) {
|
|
9
13
|
this.filename = filename;
|
|
10
14
|
this.contentType = contentType;
|
|
11
15
|
this.size = size;
|
|
12
16
|
this.url = url;
|
|
17
|
+
this.key = key;
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
// Create an InlineFile instance from a given json object.
|
|
@@ -20,7 +25,13 @@ class InlineFile {
|
|
|
20
25
|
return file;
|
|
21
26
|
}
|
|
22
27
|
|
|
23
|
-
return new InlineFile(
|
|
28
|
+
return new InlineFile(
|
|
29
|
+
obj.filename,
|
|
30
|
+
obj.contentType,
|
|
31
|
+
obj.size,
|
|
32
|
+
obj.url,
|
|
33
|
+
obj.key
|
|
34
|
+
);
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
// Create an InlineFile instance from a given dataURL
|
|
@@ -40,19 +51,49 @@ class InlineFile {
|
|
|
40
51
|
|
|
41
52
|
// Read the contents of the file. If URL is set, it will be read from the remote storage, otherwise, if dataURL is set
|
|
42
53
|
// on the instance, it will return a blob with the file contents
|
|
43
|
-
read() {
|
|
44
|
-
if (this.url) {
|
|
45
|
-
// TODO: read from store
|
|
46
|
-
}
|
|
47
|
-
|
|
54
|
+
async read() {
|
|
48
55
|
if (this._dataURL) {
|
|
49
56
|
var data = this._dataURL.split(",")[1];
|
|
50
57
|
return Buffer.from(data, "base64");
|
|
51
58
|
}
|
|
59
|
+
|
|
60
|
+
// if we don't have a key nor a dataURL, this inline file has no data
|
|
61
|
+
if (!this.key) {
|
|
62
|
+
throw new Error("invalid file data");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (isS3Storage()) {
|
|
66
|
+
const s3Client = new S3Client({
|
|
67
|
+
credentials: fromEnv(),
|
|
68
|
+
region: process.env.KEEL_REGION,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const params = {
|
|
72
|
+
Bucket: process.env.KEEL_FILES_BUCKET_NAME,
|
|
73
|
+
Key: "files/" + this.key,
|
|
74
|
+
};
|
|
75
|
+
const command = new GetObjectCommand(params);
|
|
76
|
+
const response = await s3Client.send(command);
|
|
77
|
+
return response.Body.transformToString();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// default to db storage
|
|
81
|
+
const db = useDatabase();
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
let query = db.selectFrom("keel_storage").select("data").where({
|
|
85
|
+
id: this.key,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const row = await query.executeTakeFirstOrThrow();
|
|
89
|
+
return row.data;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
throw new DatabaseError(e);
|
|
92
|
+
}
|
|
52
93
|
}
|
|
53
94
|
|
|
54
|
-
async store() {
|
|
55
|
-
const content = this.read();
|
|
95
|
+
async store(expires = null) {
|
|
96
|
+
const content = await this.read();
|
|
56
97
|
this.key = KSUID.randomSync().string;
|
|
57
98
|
|
|
58
99
|
if (isS3Storage()) {
|
|
@@ -63,7 +104,7 @@ class InlineFile {
|
|
|
63
104
|
|
|
64
105
|
const params = {
|
|
65
106
|
Bucket: process.env.KEEL_FILES_BUCKET_NAME,
|
|
66
|
-
Key: this.key,
|
|
107
|
+
Key: "files/" + this.key,
|
|
67
108
|
Body: content,
|
|
68
109
|
ContentType: this.contentType,
|
|
69
110
|
Metadata: {
|
|
@@ -71,10 +112,14 @@ class InlineFile {
|
|
|
71
112
|
},
|
|
72
113
|
};
|
|
73
114
|
|
|
115
|
+
if (expires) {
|
|
116
|
+
params.Expires = expires;
|
|
117
|
+
}
|
|
118
|
+
|
|
74
119
|
const command = new PutObjectCommand(params);
|
|
75
120
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
121
|
+
await s3Client.send(command);
|
|
122
|
+
|
|
78
123
|
return {
|
|
79
124
|
key: this.key,
|
|
80
125
|
size: this.size,
|