2022-12-15 14:29:56 +00:00
|
|
|
const std = @import("std");
|
2022-12-03 09:00:04 +00:00
|
|
|
const api = @import("api");
|
|
|
|
const http = @import("http");
|
|
|
|
const util = @import("util");
|
|
|
|
const controller_utils = @import("../../controllers.zig").helpers;
|
|
|
|
|
|
|
|
const Uuid = util.Uuid;
|
|
|
|
const DateTime = util.DateTime;
|
|
|
|
|
|
|
|
pub const drive_path = "/drive/:path*";
|
|
|
|
pub const DriveArgs = struct {
|
|
|
|
path: []const u8,
|
|
|
|
};
|
|
|
|
|
2022-12-06 09:48:36 +00:00
|
|
|
pub const get = struct {
|
2022-12-03 09:00:04 +00:00
|
|
|
pub const method = .GET;
|
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-06 09:48:36 +00:00
|
|
|
const result = try srv.driveGet(req.args.path);
|
2022-12-07 05:41:01 +00:00
|
|
|
defer util.deepFree(srv.allocator, result);
|
2022-12-06 09:48:36 +00:00
|
|
|
|
|
|
|
try res.json(.ok, result);
|
2022-12-03 09:00:04 +00:00
|
|
|
}
|
|
|
|
};
|
2022-12-03 07:44:27 +00:00
|
|
|
|
|
|
|
pub const upload = struct {
|
|
|
|
pub const method = .POST;
|
2022-12-03 09:00:04 +00:00
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
2022-12-03 07:44:27 +00:00
|
|
|
|
|
|
|
pub const Body = struct {
|
|
|
|
file: http.FormFile,
|
2022-12-03 09:00:04 +00:00
|
|
|
description: ?[]const u8 = null,
|
|
|
|
sensitive: bool = false,
|
2022-12-03 07:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
|
|
|
const f = req.body.file;
|
2022-12-07 05:41:01 +00:00
|
|
|
const result = try srv.driveUpload(.{
|
2022-12-03 15:09:29 +00:00
|
|
|
.dir = req.args.path,
|
|
|
|
.filename = f.filename,
|
|
|
|
.description = req.body.description,
|
|
|
|
.content_type = f.content_type,
|
|
|
|
.sensitive = req.body.sensitive,
|
|
|
|
}, f.data);
|
2022-12-07 05:41:01 +00:00
|
|
|
errdefer util.deepFree(srv.allocator, result);
|
2022-12-03 15:09:29 +00:00
|
|
|
|
2022-12-07 05:41:01 +00:00
|
|
|
try res.json(.created, result);
|
2022-12-03 09:00:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const delete = struct {
|
|
|
|
pub const method = .DELETE;
|
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-06 09:48:36 +00:00
|
|
|
try srv.driveDelete(req.args.path);
|
2022-12-03 09:00:04 +00:00
|
|
|
|
|
|
|
return res.json(.ok, .{});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const mkdir = struct {
|
|
|
|
pub const method = .MKCOL;
|
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-15 14:29:56 +00:00
|
|
|
var split = std.mem.splitBackwards(u8, std.mem.trim(u8, req.args.path, "/"), "/");
|
|
|
|
const name = split.first();
|
|
|
|
const parent = split.rest();
|
|
|
|
std.log.debug("{s}, {s}", .{ parent, name });
|
|
|
|
|
|
|
|
const result = try srv.driveMkdir(parent, name);
|
2022-12-07 05:41:01 +00:00
|
|
|
errdefer util.deepFree(srv.allocator, result);
|
2022-12-03 09:00:04 +00:00
|
|
|
|
2022-12-07 05:41:01 +00:00
|
|
|
try res.json(.created, result);
|
2022-12-03 09:00:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const update = struct {
|
|
|
|
pub const method = .PUT;
|
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
|
|
|
|
2022-12-06 09:48:36 +00:00
|
|
|
// TODO: Validate that unhandled fields are equivalent to ones in the object
|
|
|
|
pub const allow_unknown_fields_in_body = true;
|
2022-12-03 09:00:04 +00:00
|
|
|
pub const Body = struct {
|
2022-12-07 05:41:01 +00:00
|
|
|
meta: struct {
|
|
|
|
filename: ?[]const u8 = null,
|
|
|
|
description: ?[]const u8 = null,
|
|
|
|
content_type: ?[]const u8 = null,
|
|
|
|
sensitive: ?bool = null,
|
|
|
|
},
|
2022-12-03 09:00:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2023-01-04 19:25:08 +00:00
|
|
|
try srv.driveUpdate(req.args.path, .{
|
2022-12-07 05:41:01 +00:00
|
|
|
.filename = req.body.meta.filename,
|
|
|
|
.description = req.body.meta.description,
|
|
|
|
.content_type = req.body.meta.content_type,
|
|
|
|
.sensitive = req.body.meta.sensitive,
|
2022-12-06 09:48:36 +00:00
|
|
|
});
|
2023-01-04 19:25:08 +00:00
|
|
|
|
|
|
|
const result = try srv.driveGet(req.args.path);
|
2022-12-07 05:41:01 +00:00
|
|
|
defer util.deepFree(srv.allocator, result);
|
|
|
|
try res.json(.ok, result);
|
2022-12-03 09:00:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const move = struct {
|
|
|
|
pub const method = .MOVE;
|
|
|
|
pub const path = drive_path;
|
|
|
|
pub const Args = DriveArgs;
|
|
|
|
|
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-06 09:48:36 +00:00
|
|
|
const destination = req.headers.get("Destination") orelse return error.NoDestination;
|
2022-12-03 09:00:04 +00:00
|
|
|
|
2023-01-04 19:25:08 +00:00
|
|
|
try srv.driveMove(req.args.path, destination);
|
|
|
|
|
|
|
|
const result = try srv.driveGet(req.args.path);
|
2022-12-07 05:41:01 +00:00
|
|
|
defer util.deepFree(srv.allocator, result);
|
2022-12-03 07:44:27 +00:00
|
|
|
|
2022-12-06 09:48:36 +00:00
|
|
|
try res.headers.put("Location", destination);
|
2022-12-07 05:41:01 +00:00
|
|
|
try res.json(.created, result);
|
2022-12-03 07:44:27 +00:00
|
|
|
}
|
|
|
|
};
|