diff options
Diffstat (limited to 'src/storage/image-manager/root.zig')
| -rw-r--r-- | src/storage/image-manager/root.zig | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/storage/image-manager/root.zig b/src/storage/image-manager/root.zig new file mode 100644 index 0000000..7742df2 --- /dev/null +++ b/src/storage/image-manager/root.zig @@ -0,0 +1,66 @@ +const std = @import("std"); + +const Storage = @import("../root.zig"); +pub const Image = @import("image.zig"); + +const Self = @This(); + +pub const empty: Self = .{}; + +items: std.ArrayList(Image) = .empty, +rw_lock: std.Thread.RwLock = .{}, + +pub const LockedImages = struct { + images: []Image, + rw_lock: *std.Thread.RwLock, + + pub fn deinit(self: *const @This()) void { + self.rw_lock.unlockShared(); + } +}; + +pub fn init( + self: *Self, + storage: *Storage, +) !void { + var dir = storage.dir.openDir("image", .{ .iterate = true }) catch blk: { + try storage.dir.makeDir("image"); + break :blk try storage.dir.openDir("image", .{ .iterate = true }); + }; + defer dir.close(); + + var iterator = dir.iterate(); + + errdefer self.items.deinit(storage.allocator); + + while (try iterator.next()) |entry| { + if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".jpg")) { + try self.items.append( + storage.allocator, + try .init(storage.allocator, entry.name[0..entry.name.len - 4]) + ); + } + } +} + +pub fn add( + self: *Self, + storage: *Storage, + reader: *std.Io.Reader, + size: usize, +) !void { + self.rw_lock.lock(); + defer self.rw_lock.unlock(); + + try self.items.append( + storage.allocator, + try Image.new(storage, reader, size), + ); +} + +pub fn list(self: *Self) LockedImages { + return .{ + .images = self.items.items, + .rw_lock = &self.rw_lock, + }; +} |