Implement TR-069 Upload method
This commit is contained in:
1 parent
d29448e3a7
commit
dae521ae89
22 files changed
+1016
-68
No files matched your search
@@ -58,6 +58,10 @@ export async function deleteResource(
|
||||
await db.deleteFile(id);
|
||||
break;
|
||||
|
||||
case "uploads":
|
||||
await db.deleteUpload(id);
|
||||
break;
|
||||
|
||||
case "faults":
|
||||
await deleteFault(id);
|
||||
break;
|
||||
|
||||
+38
-1
@@ -49,6 +49,7 @@ const RESOURCE_IDS = {
|
||||
presets: "_id",
|
||||
provisions: "_id",
|
||||
files: "_id",
|
||||
uploads: "_id",
|
||||
virtualParameters: "_id",
|
||||
config: "_id",
|
||||
permissions: "_id",
|
||||
@@ -62,6 +63,7 @@ const resources = {
|
||||
presets: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
provisions: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
files: 0 | RESOURCE_DELETE,
|
||||
uploads: 0 | RESOURCE_DELETE,
|
||||
virtualParameters: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
config: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
permissions: 0 | RESOURCE_DELETE | RESOURCE_PUT,
|
||||
@@ -361,7 +363,7 @@ for (const [resource, flags] of Object.entries(resources)) {
|
||||
});
|
||||
|
||||
if (flags & RESOURCE_DELETE) {
|
||||
router.delete(`/${resource}/:id`, async (ctx) => {
|
||||
router.delete(`/${resource}/:id(.+)`, async (ctx) => {
|
||||
const authorizer: Authorizer = ctx.state.authorizer;
|
||||
const log = {
|
||||
message: `Delete ${resource}`,
|
||||
@@ -478,6 +480,41 @@ router.put("/files/:id", async (ctx) => {
|
||||
ctx.body = "";
|
||||
});
|
||||
|
||||
router.get("/uploads/blob/:id(.+)", async (ctx) => {
|
||||
const authorizer: Authorizer = ctx.state.authorizer;
|
||||
const resource = "uploads";
|
||||
const id = ctx.params.id;
|
||||
|
||||
const log = {
|
||||
message: `Download ${resource} blob`,
|
||||
context: ctx,
|
||||
id: id,
|
||||
};
|
||||
1;
|
||||
|
||||
const filter = and(authorizer.getFilter(resource, 2), [
|
||||
"=",
|
||||
["PARAM", RESOURCE_IDS[resource]],
|
||||
id,
|
||||
]);
|
||||
|
||||
if (!authorizer.hasAccess(resource, 2)) {
|
||||
logUnauthorizedWarning(log);
|
||||
return void (ctx.status = 403);
|
||||
}
|
||||
|
||||
const res = await db.query(resource, filter);
|
||||
if (!res[0]) return void (ctx.status = 404);
|
||||
|
||||
ctx.type = "application/octet-stream";
|
||||
ctx.attachment(id.replace(/\//g, "_"));
|
||||
ctx.body = new stream.PassThrough();
|
||||
|
||||
const downloadStream = await db.getUploadBlob(id);
|
||||
downloadStream.pipe(ctx.body);
|
||||
logger.accessInfo(log);
|
||||
});
|
||||
|
||||
router.post("/devices/:id/tasks", async (ctx) => {
|
||||
const authorizer: Authorizer = ctx.state.authorizer;
|
||||
const log = {
|
||||
|
||||
@@ -28,6 +28,7 @@ import { minimize } from "../common/boolean-expression";
|
||||
|
||||
const RESOURCE_COLLECTION = {
|
||||
files: "fs.files",
|
||||
uploads: "uploads.files",
|
||||
};
|
||||
|
||||
let db: Db;
|
||||
@@ -112,6 +113,8 @@ export function query(
|
||||
docs = docs.map((d) => mongodbFunctions.flattenPreset(d));
|
||||
else if (resource === "files")
|
||||
docs = docs.map((d) => mongodbFunctions.flattenFile(d));
|
||||
else if (resource === "uploads")
|
||||
docs = docs.map((d) => mongodbFunctions.flattenUpload(d));
|
||||
return resolve(docs);
|
||||
});
|
||||
} else {
|
||||
@@ -126,6 +129,8 @@ export function query(
|
||||
doc = mongodbFunctions.flattenPreset(doc);
|
||||
else if (resource === "files")
|
||||
doc = mongodbFunctions.flattenFile(doc);
|
||||
else if (resource === "uploads")
|
||||
doc = mongodbFunctions.flattenUpload(doc);
|
||||
callback(doc);
|
||||
},
|
||||
(err) => {
|
||||
@@ -326,6 +331,16 @@ export function deleteFile(filename: string): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteUpload(filename: string): Promise<void> {
|
||||
const bucket = new GridFSBucket(db, { bucketName: "uploads" });
|
||||
await bucket.delete(filename as any);
|
||||
}
|
||||
|
||||
export async function getUploadBlob(filename: string): Promise<Readable> {
|
||||
const bucket = new GridFSBucket(db, { bucketName: "uploads" });
|
||||
return bucket.openDownloadStreamByName(filename);
|
||||
}
|
||||
|
||||
export function deleteFault(id: string): Promise<void> {
|
||||
return deleteResource("faults", id);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user