-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (72 loc) · 2.19 KB
/
index.js
File metadata and controls
83 lines (72 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const AWS = require("aws-sdk");
const { customAlphabet } = require('nanoid');
const validator = require("validator")
const documentClient = new AWS.DynamoDB.DocumentClient();
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwyz', 7)
const tableName = "content";
const defaultTTL = 604800; // Default TTL in seconds (7 days)
const minimumTTL = 3600; // Minum TTL in seconds (1 hour)
exports.handler = async(event) => {
const body = JSON.parse(event['body']);
var id = "";
var url = validator.isURL(body.data);
if (!body.data) {
return {
statusCode: 400,
body: JSON.stringify({error: "Empty data"})
};
}
!body["id"] ?
id = nanoid() : id = body["id"];
const item = {
id,
data: body["data"],
buid: body["buid"],
isEncrypted: body['isEncrypted'] || false,
url
};
// Set TTL based on the "expire" flag in the request
let ttl;
if (body.hasOwnProperty('expire') && body['expire'] === false) {
ttl = null; // No TTL attribute when 'expire' is explicitly false
} else {
// Check if a custom TTL is provided and meets the minimum requirement
if (body.hasOwnProperty('ttl') && body['ttl'] >= minimumTTL) {
ttl = Math.floor(Date.now() / 1000) + body['ttl'];
} else {
// Default to 7 days if no valid custom TTL is provided
ttl = Math.floor(Date.now() / 1000) + defaultTTL;
}
}
if (ttl){
item.ttl = ttl;
}
const params = {
TableName: tableName,
Item: item,
ConditionExpression: "#id <> :id",
ExpressionAttributeNames: {
"#id": "id"
},
ExpressionAttributeValues: {
":id": id
}
};
try {
await documentClient.put(params).promise();
}
catch (e) {
return {
statusCode: 400,
body: JSON.stringify({ error: e })
};
}
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(params.Item),
};
return response;
};