initial commit
This commit is contained in:
12
functions/cleanShares/package.json
Normal file
12
functions/cleanShares/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "appwrite-function",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"node-appwrite": "^5.0.0"
|
||||
}
|
||||
}
|
||||
39
functions/cleanShares/src/index.js
Normal file
39
functions/cleanShares/src/index.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const sdk = require("node-appwrite")
|
||||
|
||||
module.exports = async function (req, res) {
|
||||
const client = new sdk.Client();
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
const deletedShares = (await database.listDocuments("shares", [sdk.Query.lesser("expiresAt",Date.now())],
|
||||
100)).documents;
|
||||
console.log(deletedShares)
|
||||
for (const share of deletedShares) {
|
||||
database.deleteDocument("shares", share.$id)
|
||||
if (share.securityID != null) {
|
||||
database.deleteDocument("shareSecurity", share.securityID)
|
||||
}
|
||||
storage.deleteBucket(share.$id)
|
||||
console.log("deleted" + share.$id)
|
||||
}
|
||||
|
||||
res.json({
|
||||
status: "done"
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
12
functions/createShare/package.json
Normal file
12
functions/createShare/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "appwrite-function",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"node-appwrite": "^5.0.0"
|
||||
}
|
||||
}
|
||||
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,
|
||||
}
|
||||
12
functions/finishShare/package.json
Normal file
12
functions/finishShare/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "appwrite-function",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"node-appwrite": "^5.0.0"
|
||||
}
|
||||
}
|
||||
22
functions/finishShare/src/index.js
Normal file
22
functions/finishShare/src/index.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const sdk = require("node-appwrite")
|
||||
|
||||
module.exports = async function (req, res) {
|
||||
const client = new sdk.Client();
|
||||
|
||||
let database = new sdk.Database(client);
|
||||
|
||||
client
|
||||
.setEndpoint(req.env["APPWRITE_FUNCTION_ENDPOINT"])
|
||||
.setProject(req.env["APPWRITE_FUNCTION_PROJECT_ID"])
|
||||
.setKey(req.env["APPWRITE_FUNCTION_API_KEY"])
|
||||
.setSelfSigned(true);
|
||||
|
||||
|
||||
const payload = JSON.parse(req.payload);
|
||||
database.updateDocument("shares", payload.id, {
|
||||
enabled: true
|
||||
})
|
||||
res.json({
|
||||
id: payload.id,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user