File dereference endpoint

This commit is contained in:
jaina heartles 2022-12-06 02:53:41 -08:00
parent 6f4882794a
commit 9e66ef441b
2 changed files with 38 additions and 0 deletions

View file

@ -159,6 +159,11 @@ pub const DriveGetResult = union(services.drive.Kind) {
},
};
pub const FileResult = struct {
meta: services.files.FileUpload,
data: []const u8,
};
pub fn isAdminSetup(db: sql.Db) !bool {
_ = services.communities.adminCommunityId(db) catch |err| switch (err) {
error.NotFound => return false,
@ -616,5 +621,15 @@ fn ApiConn(comptime DbConn: type) type {
std.log.debug("{}", .{entry.id});
try services.files.update(self.db, entry.file_id orelse return error.NotAFile, meta, self.allocator);
}
pub fn fileDereference(self: *Self, id: Uuid) !FileResult {
const meta = try services.files.get(self.db, id, self.allocator);
errdefer util.deepFree(self.allocator, meta);
return FileResult{
.meta = meta,
.data = try services.files.deref(self.allocator, id),
};
}
};
}

View file

@ -1,4 +1,5 @@
const std = @import("std");
const util = @import("util");
const controllers = @import("../controllers.zig");
pub const routes = .{
@ -7,6 +8,7 @@ pub const routes = .{
controllers.apiEndpoint(login),
controllers.apiEndpoint(global_timeline),
controllers.apiEndpoint(cluster.overview),
controllers.apiEndpoint(media),
};
const index = struct {
@ -87,3 +89,24 @@ const cluster = struct {
}
};
};
const media = struct {
pub const path = "/media/:id";
pub const method = .GET;
pub const Args = struct {
id: util.Uuid,
};
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const result = try srv.fileDereference(req.args.id);
defer util.deepFree(srv.allocator, result);
try res.headers.put("Content-Type", result.meta.content_type orelse "application/octet-stream");
var stream = try res.open(.ok);
defer stream.close();
try stream.writer().writeAll(result.data);
try stream.finish();
}
};