mirror of
https://github.com/swissmakers/swiss-datashare.git
synced 2026-04-21 06:03:13 +02:00
initial commit
This commit is contained in:
58
functions/createShare/src/index.js
Normal file
58
functions/createShare/src/index.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const sdk = require("node-appwrite")
|
||||
const util = require("./util")
|
||||
|
||||
module.exports = async function (req, res) {
|
||||
const client = new sdk.Client();
|
||||
|
||||
// You can remove services you don't use
|
||||
let database = new sdk.Database(client);
|
||||
let storage = new sdk.Storage(client);
|
||||
|
||||
client
|
||||
.setEndpoint(req.env["APPWRITE_FUNCTION_ENDPOINT"])
|
||||
.setProject(req.env["APPWRITE_FUNCTION_PROJECT_ID"])
|
||||
.setKey(req.env["APPWRITE_FUNCTION_API_KEY"])
|
||||
.setSelfSigned(true);
|
||||
|
||||
// Payload (HTTP body) that was sent
|
||||
const payload = JSON.parse(req.payload);
|
||||
|
||||
// User Id from the user which created a share
|
||||
const userId = req.env["APPWRITE_FUNCTION_USER_ID"];
|
||||
|
||||
let securityDocumentId;
|
||||
|
||||
// If a security property was given create a document in the Share Security collection
|
||||
if (Object.getOwnPropertyNames(payload.security).length != 0) {
|
||||
securityDocumentId = (
|
||||
await database.createDocument(
|
||||
"shareSecurity",
|
||||
"unique()",
|
||||
{ maxVisitors: payload.security.maxVisitors, password: payload.security.password ? util.hashPassword(payload.security.password, payload.id) : undefined, },
|
||||
[]
|
||||
)
|
||||
).$id;
|
||||
}
|
||||
|
||||
// Create the storage bucket
|
||||
await storage.createBucket(
|
||||
payload.id,
|
||||
`Share-${payload.id}`,
|
||||
"bucket",
|
||||
["role:all"],
|
||||
[`user:${userId}`],
|
||||
)
|
||||
|
||||
const expiration = Date.now() + (payload.expiration * 60 * 1000)
|
||||
|
||||
// Create document in Shares collection
|
||||
await database.createDocument("shares", payload.id, {
|
||||
securityID: securityDocumentId,
|
||||
createdAt: Date.now(),
|
||||
expiresAt: expiration,
|
||||
});
|
||||
|
||||
res.json({
|
||||
id: payload.id,
|
||||
});
|
||||
};
|
||||
9
functions/createShare/src/util.js
Normal file
9
functions/createShare/src/util.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const { scryptSync } = require("crypto");
|
||||
|
||||
const hashPassword = (password, salt) => {
|
||||
return scryptSync(password, salt, 64).toString("hex");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hashPassword,
|
||||
}
|
||||
Reference in New Issue
Block a user