diff options
Diffstat (limited to 'src/storage/image-manager')
| -rw-r--r-- | src/storage/image-manager/exif.zig | 57 | ||||
| -rw-r--r-- | src/storage/image-manager/image.zig | 110 | ||||
| -rw-r--r-- | src/storage/image-manager/root.zig | 138 |
3 files changed, 0 insertions, 305 deletions
diff --git a/src/storage/image-manager/exif.zig b/src/storage/image-manager/exif.zig deleted file mode 100644 index 88e9b48..0000000 --- a/src/storage/image-manager/exif.zig +++ /dev/null @@ -1,57 +0,0 @@ -const std = @import("std"); -const exif = @cImport(@cInclude("libexif/exif-data.h")); -const cstd = @cImport(@cInclude("time.h")); - -const tags = [_]c_uint { - exif.EXIF_TAG_DATE_TIME, - exif.EXIF_TAG_DATE_TIME_ORIGINAL, - exif.EXIF_TAG_DATE_TIME_DIGITIZED, -}; - -pub fn get_date_time(path: [*:0]const u8) ?i64 { - const exif_data = exif.exif_data_new_from_file(path); - defer exif.exif_data_unref(exif_data); - - if (exif_data == 0) { - return null; - } - - var entry: ?[*c]exif.struct__ExifEntry = null; - for (0..exif.EXIF_IFD_COUNT) |index| { - for (tags) |tag| { - entry = exif.exif_content_get_entry((exif_data.*).ifd[index], tag); - - if (entry) |_| { break; } - } - - if (entry) |_| { break; } - } - - if (entry == 0) { - return null; - } - - if (entry) |e| { - const c_data = (e.*).data; - return parse_date(c_data); - } - - return null; -} - -extern fn strptime( - s: [*c]const u8, - format: [*c]const u8, - tm: *cstd.tm, -) ?*const u8; - -extern fn mktime(tm: *cstd.tm) i64; - -fn parse_date(date: [*:0]const u8) ?i64 { - var tm: cstd.tm = std.mem.zeroes(cstd.tm); - if (strptime(date, "%Y:%m:%d %H:%M:%S", &tm)) |_| { - return mktime(&tm); - } - - return null; -} diff --git a/src/storage/image-manager/image.zig b/src/storage/image-manager/image.zig deleted file mode 100644 index bdac5c7..0000000 --- a/src/storage/image-manager/image.zig +++ /dev/null @@ -1,110 +0,0 @@ -const std = @import("std"); -const Storage = @import("../root.zig"); - -const exif = @import("exif.zig"); - -const id_size = 32; - -const log = std.log.scoped(.image); - -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..].*; -} - -const Self = @This(); - -id: []const u8, -timestamp: ?i64, - -pub fn new( - storage: *Storage, - reader: *std.Io.Reader, - size: usize, -) !Self { - var dir = try storage.dir.openDir("image", .{}); - defer dir.close(); - - var self: Self = undefined; - - self.id = try storage.allocator.dupe(u8, &new_id()); - - var file_name: [id_size + 4]u8 = undefined; - @memcpy(file_name[0..id_size], self.id); - @memcpy(file_name[id_size..], ".jpg"); - - var image_file = try dir.createFile(&file_name, .{}); - defer image_file.close(); - - var buffer: [1024]u8 = undefined; - var file_writer = image_file.writer(&buffer); - - try reader.streamExact(&file_writer.interface, size); - - try file_writer.interface.flush(); - - log.info("uploaded {s} [{} bytes]", .{ self.id, size }); - - self.timestamp = try load_timestamp(storage, self.id); - - return self; -} - -pub fn delete(self: *Self, storage: *Storage) !void { - var dir = try storage.dir.openDir("image", .{}); - defer dir.close(); - - var file_name: [id_size + 4]u8 = undefined; - @memcpy(file_name[0..id_size], self.id); - @memcpy(file_name[id_size..], ".jpg"); - - try dir.deleteFile(&file_name); -} - -pub fn init(storage: *Storage, id: []const u8) !Self { - return .{ - .id = try storage.allocator.dupe(u8, id), - .timestamp = try load_timestamp(storage, id), - }; -} - -pub fn file(self: *Self, storage: *Storage) !std.fs.File { - 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], self.id); - @memcpy(file_name[id_size..], ".jpg"); - - return dir.openFile(&file_name, .{}); -} - -pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { - allocator.free(self.id); -} - -fn load_timestamp(storage: *Storage, id: []const u8) !?i64 { - 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], id); - @memcpy(file_name[id_size..], ".jpg"); - - var path_buffer: [std.fs.max_path_bytes:0]u8 = std.mem.zeroes([std.fs.max_path_bytes:0]u8); - const path = try dir.realpath(&file_name, &path_buffer); - - return exif.get_date_time(@ptrCast(path)); -} diff --git a/src/storage/image-manager/root.zig b/src/storage/image-manager/root.zig deleted file mode 100644 index efc216f..0000000 --- a/src/storage/image-manager/root.zig +++ /dev/null @@ -1,138 +0,0 @@ -const std = @import("std"); - -const memora = @import("memora"); -const Storage = memora.Storage; -pub const Image = @import("image.zig"); - -const Self = @This(); - -pub const empty: Self = .{}; - -const Timestamp = struct { - index: usize, - node: std.DoublyLinkedList.Node, - - pub fn next(self: *Timestamp) ?*Timestamp { - return @fieldParentPtr("node", self.node.next orelse return null); - } - - pub fn prev(self: *Timestamp) ?*Timestamp { - return @fieldParentPtr("node", self.node.prev orelse return null); - } -}; - -items: std.ArrayList(Image) = .empty, -rw_lock: std.Thread.RwLock = .{}, -timestamp_order: std.DoublyLinkedList = .{}, - -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.add( - storage.allocator, - try .init(storage, entry.name[0..entry.name.len - 4]) - ); - } - } -} - -pub fn add(self: *Self, allocator: std.mem.Allocator, image: Image) !void { - self.rw_lock.lock(); - defer self.rw_lock.unlock(); - - try self.items.append( - allocator, - image, - ); - - var current = self.timestamp_order.first; - - while (current) |c| { - const index = @as(*Timestamp, @fieldParentPtr("node", c)).index; - if (image.timestamp orelse 0 > self.items.items[index].timestamp orelse 0) { - const timestamp = try allocator.create(Timestamp); - timestamp.index = self.items.items.len - 1; - self.timestamp_order.insertBefore(c, ×tamp.node); - return; - } - - current = c.next; - } - - const timestamp = try allocator.create(Timestamp); - timestamp.index = self.items.items.len - 1; - self.timestamp_order.append(×tamp.node); - return; -} - -pub fn save( - self: *Self, - storage: *Storage, - reader: *std.Io.Reader, - size: usize, -) !void { - return self.add(storage.allocator, try Image.new(storage, reader, size)); -} - -pub fn list(self: *Self) memora.locked.Shared([]Image) { - self.rw_lock.lockShared(); - return .{ - .value = self.items.items, - .rw_lock = &self.rw_lock, - }; -} - -pub fn first_by_timestamp(self: *Self) memora.locked.Shared(?*Timestamp) { - self.rw_lock.lockShared(); - return .{ - .value = if (self.timestamp_order.first) |first| @fieldParentPtr("node", first) - else null, - .rw_lock = &self.rw_lock, - }; -} - -pub fn delete(self: *Self, storage: *Storage, id: []const u8) !void { - self.rw_lock.lock(); - defer self.rw_lock.unlock(); - - const index = index: { - for (self.items.items, 0..) |image, index| { - if (std.mem.eql(u8, image.id, id)) { - break :index index; - } - } - break :index null; - }; - - if (index) |idx| { - try self.items.items[idx].delete(storage); - const old_index = self.items.items.len - 1; - _ = self.items.swapRemove(idx); - - var current: ?*Timestamp = if (self.timestamp_order.first) |c| @fieldParentPtr("node", c) else null; - - while (current) |c| { - if (c.index == idx) { - self.timestamp_order.remove(&c.node); - storage.allocator.destroy(c); - } else if (c.index == old_index) { - c.index = idx; - } - - current = c.next(); - } - } -} |