From af396a0cb696b618d8af64e18990c790739ef984 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 17 Dec 2022 07:46:54 -0800 Subject: [PATCH 1/4] Basic upload form --- src/main/controllers/web.zig | 29 ++++++++++++++++++- .../controllers/web/drive/directory.tmpl.html | 24 +++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/controllers/web.zig b/src/main/controllers/web.zig index 5ec6d55..993b773 100644 --- a/src/main/controllers/web.zig +++ b/src/main/controllers/web.zig @@ -281,6 +281,7 @@ const drive = struct { const Action = enum { mkdir, delete, + upload, }; pub const body_tag_from_query_param = "action"; @@ -289,9 +290,15 @@ const drive = struct { name: []const u8, }, delete: struct {}, + upload: struct { + file: http.FormFile, + sensitive: bool = false, + description: []const u8 = "", + }, }; pub fn handler(req: anytype, res: anytype, srv: anytype) !void { + const trimmed_path = std.mem.trim(u8, req.args.path, "/"); switch (req.body) { .mkdir => |body| { _ = try srv.driveMkdir(req.args.path, body.name); @@ -300,7 +307,6 @@ const drive = struct { try servePage(req, res, srv); }, .delete => { - const trimmed_path = std.mem.trim(u8, req.args.path, "/"); _ = try srv.driveDelete(trimmed_path); const dir = trimmed_path[0 .. std.mem.lastIndexOfScalar(u8, trimmed_path, '/') orelse trimmed_path.len]; @@ -312,6 +318,27 @@ const drive = struct { try res.headers.put("Location", url); return res.status(.see_other); }, + .upload => |body| { + const entry = try srv.driveUpload( + .{ + .filename = body.file.filename, + .dir = trimmed_path, + .description = body.description, + .content_type = body.file.content_type, + .sensitive = body.sensitive, + }, + body.file.data, + ); + defer util.deepFree(srv.allocator, entry); + + const url = try std.fmt.allocPrint(srv.allocator, "{s}/drive/{s}", .{ + req.mount_path, + std.mem.trim(u8, entry.file.path, "/"), + }); + defer srv.allocator.free(url); + try res.headers.put("Location", url); + return res.status(.see_other); + }, } } }; diff --git a/src/main/controllers/web/drive/directory.tmpl.html b/src/main/controllers/web/drive/directory.tmpl.html index 39f4b2d..99ae73c 100644 --- a/src/main/controllers/web/drive/directory.tmpl.html +++ b/src/main/controllers/web/drive/directory.tmpl.html @@ -37,6 +37,30 @@ + {#for .dir.children.? |$child| =} From 71a03b30f0e54c4596ee912ab793155867a0dd89 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 17 Dec 2022 12:05:08 -0800 Subject: [PATCH 2/4] Allow printing optionals --- src/template/lib.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/template/lib.zig b/src/template/lib.zig index 4e8803c..4698cab 100644 --- a/src/template/lib.zig +++ b/src/template/lib.zig @@ -210,6 +210,7 @@ fn print(writer: anytype, arg: anytype) !void { if (T == void) return; if (comptime std.meta.trait.isZigString(T)) return htmlEscape(writer, arg); if (comptime std.meta.trait.isNumber(T)) return std.fmt.format(writer, "{}", .{arg}); + if (comptime std.meta.trait.is(.Optional)(T)) return if (arg) |a| try print(writer, a); try std.fmt.format(writer, "{}", .{arg}); } From f35d52f2877d2640ec35f8e540d5f0b1e6548804 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 17 Dec 2022 12:05:17 -0800 Subject: [PATCH 3/4] Basic file details page --- src/main/controllers/web.zig | 20 +++++- src/main/controllers/web/drive/file.tmpl.html | 67 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/controllers/web/drive/file.tmpl.html diff --git a/src/main/controllers/web.zig b/src/main/controllers/web.zig index 993b773..d176239 100644 --- a/src/main/controllers/web.zig +++ b/src/main/controllers/web.zig @@ -233,6 +233,7 @@ const user_details = struct { const drive = struct { const dir_tmpl = @embedFile("./web/drive/directory.tmpl.html"); + const file_tmpl = @embedFile("./web/drive/file.tmpl.html"); fn servePage(req: anytype, res: anytype, srv: anytype) !void { const info = try srv.driveGet(req.args.path); defer util.deepFree(srv.allocator, info); @@ -246,6 +247,14 @@ const drive = struct { try breadcrumbs.append(if (p.len != 0) p else continue); } + // TODO: put this into the db layer + const FileClass = enum { + image, + video, + audio, + other, + }; + switch (info) { .dir => |dir| try res.template(.ok, srv, dir_tmpl, .{ .dir = dir, @@ -253,7 +262,16 @@ const drive = struct { .mount_path = req.mount_path, .base_drive_path = "drive", }), - else => unreachable, + .file => |file| try res.template(.ok, srv, file_tmpl, .{ + .file = file, + .breadcrumbs = breadcrumbs.items, + .mount_path = req.mount_path, + .base_drive_path = "drive", + .class = if (std.mem.eql(u8, file.meta.content_type orelse "", "image/jpeg")) + FileClass.image + else + FileClass.other, + }), } } diff --git a/src/main/controllers/web/drive/file.tmpl.html b/src/main/controllers/web/drive/file.tmpl.html new file mode 100644 index 0000000..f3dd64d --- /dev/null +++ b/src/main/controllers/web/drive/file.tmpl.html @@ -0,0 +1,67 @@ +
+ + + + +
+

{.file.name.?}

+
+ {#if @isTag(.class, image) =} + + {#elif @isTag(.class, video) =} +
+
+

Metadata

+ +

Drive Path

+
{.file.path}
+ +

Filename

+
{.file.meta.filename}
+ +

Content Type

+
{.file.meta.content_type}
+ +

Sensitive?

+
{.file.meta.sensitive}
+ +

Description

+
{.file.meta.description}
+
+
+
From 951bb90ad8e7ff424ce0317ba054c402ee08a8df Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 17 Dec 2022 12:10:17 -0800 Subject: [PATCH 4/4] Fix link href for upload button --- src/main/controllers/web/drive/directory.tmpl.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/controllers/web/drive/directory.tmpl.html b/src/main/controllers/web/drive/directory.tmpl.html index 99ae73c..db94cbd 100644 --- a/src/main/controllers/web/drive/directory.tmpl.html +++ b/src/main/controllers/web/drive/directory.tmpl.html @@ -38,7 +38,7 @@