diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 15:12:19 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 15:12:19 +0100 |
| commit | 0016aaa197697ec5ff38dfb3f63ac8b6f74b48e0 (patch) | |
| tree | 688930dcc978bcadfc01dac4ff09908452d802a4 /src | |
| parent | 69488029a8eafc55d42ae7e65ba8ddeebcfb8454 (diff) | |
backend: add image upload
Diffstat (limited to 'src')
| -rw-r--r-- | src/routes/api/image/root.zig | 3 | ||||
| -rw-r--r-- | src/routes/api/image/upload.zig | 15 | ||||
| -rw-r--r-- | src/routes/api/root.zig | 1 | ||||
| -rw-r--r-- | src/storage/image.zig | 39 | ||||
| -rw-r--r-- | src/storage/root.zig | 1 |
5 files changed, 59 insertions, 0 deletions
diff --git a/src/routes/api/image/root.zig b/src/routes/api/image/root.zig new file mode 100644 index 0000000..0ad1960 --- /dev/null +++ b/src/routes/api/image/root.zig @@ -0,0 +1,3 @@ +const HandlerInfo = @import("../../handler-info.zig"); + +pub const upload: HandlerInfo = .from_type(@import("upload.zig")); diff --git a/src/routes/api/image/upload.zig b/src/routes/api/image/upload.zig new file mode 100644 index 0000000..7cd5cf1 --- /dev/null +++ b/src/routes/api/image/upload.zig @@ -0,0 +1,15 @@ +const std = @import("std"); +const Context = @import("../../context.zig"); +const Storage = @import("../../../storage/root.zig"); + +const log = std.log.scoped(.image_upload); + +pub const access = .everyone; + +pub fn post(ctx: *Context) !void { + if (ctx.request.head.content_length) |length| { + var buffer: [1024]u8 = undefined; + const reader = try ctx.request.readerExpectContinue(&buffer); + try Storage.Image.new(ctx.storage, reader, length); + } +} diff --git a/src/routes/api/root.zig b/src/routes/api/root.zig index e3c5d6f..6d5745f 100644 --- a/src/routes/api/root.zig +++ b/src/routes/api/root.zig @@ -1,2 +1,3 @@ pub const auth = @import("auth/root.zig"); pub const session = @import("session/root.zig"); +pub const image = @import("image/root.zig"); diff --git a/src/storage/image.zig b/src/storage/image.zig new file mode 100644 index 0000000..73bca84 --- /dev/null +++ b/src/storage/image.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const Storage = @import("root.zig"); + +const id_size = 32; + +fn new_id() [id_size]u8 { + var buffer: [id_size]u8 = undefined; + var raw_buffer: [id_size / 2]u8 = undefined; + std.crypto.random.bytes(&raw_buffer); + + var writer = std.Io.Writer.fixed(buffer[0..]); + writer.print("{x}", .{raw_buffer}) catch unreachable; + + return buffer[0..].*; +} + +pub fn new( + storage: *Storage, + reader: *std.Io.Reader, + size: usize, +) !void { + var dir = storage.dir.openDir("image", .{}) catch blk: { + try storage.dir.makeDir("image"); + break :blk try storage.dir.openDir("image", .{}); + }; + defer dir.close(); + + var file_name: [id_size+4]u8 = undefined; + @memcpy(file_name[0..id_size], &new_id()); + @memcpy(file_name[id_size..], ".jpg"); + + var file = try dir.createFile(&file_name, .{}); + defer file.close(); + + var buffer: [1024]u8 = undefined; + var file_writer = file.writer(&buffer); + + try reader.streamExact(&file_writer.interface, size); +} diff --git a/src/storage/root.zig b/src/storage/root.zig index f1a753c..c121ca9 100644 --- a/src/storage/root.zig +++ b/src/storage/root.zig @@ -3,6 +3,7 @@ const config = @import("../config.zig"); const prompt = @import("../prompt.zig"); pub const User = @import("user.zig"); +pub const Image = @import("image.zig"); pub const SessionManager = @import("session-manager/root.zig"); pub const Session = SessionManager.Session; |