aboutsummaryrefslogtreecommitdiff
path: root/src/storage/image-manager/image.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-11-19 09:15:49 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-11-19 09:15:49 +0100
commit4c06eb64cbed3562e428ce59857d1763098638f3 (patch)
treecc9c8164e76cd48e1dd4ef963329dcfa3c1b152f /src/storage/image-manager/image.zig
parent6201307fecf8398a1b53bf276bc08bfbb3524899 (diff)
allow images to upload and sort if according to their datetime
Diffstat (limited to 'src/storage/image-manager/image.zig')
-rw-r--r--src/storage/image-manager/image.zig38
1 files changed, 32 insertions, 6 deletions
diff --git a/src/storage/image-manager/image.zig b/src/storage/image-manager/image.zig
index 91a094e..b11b7b1 100644
--- a/src/storage/image-manager/image.zig
+++ b/src/storage/image-manager/image.zig
@@ -1,6 +1,8 @@
const std = @import("std");
const Storage = @import("../root.zig");
+const exif = @import("exif.zig");
+
const id_size = 32;
const log = std.log.scoped(.image);
@@ -19,6 +21,7 @@ fn new_id() [id_size]u8 {
const Self = @This();
id: []const u8,
+timestamp: ?i64,
pub fn new(
storage: *Storage,
@@ -28,8 +31,11 @@ pub fn new(
var dir = try storage.dir.openDir("image", .{});
defer dir.close();
- var file_name: [id_size+4]u8 = undefined;
- const self: Self = .{ .id = &new_id() };
+ 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");
@@ -43,14 +49,17 @@ pub fn new(
try file_writer.interface.flush();
- log.info("uploaded {s} [{} bytes]", .{self.id, size});
+ log.info("uploaded {s} [{} bytes]", .{ self.id, size });
+
+ self.timestamp = try load_timestamp(storage, self.id);
return self;
}
-pub fn init(allocator: std.mem.Allocator, id: []const u8) !Self {
+pub fn init(storage: *Storage, id: []const u8) !Self {
return .{
- .id = try allocator.dupe(u8, id),
+ .id = try storage.allocator.dupe(u8, id),
+ .timestamp = try load_timestamp(storage, id),
};
}
@@ -61,7 +70,7 @@ pub fn file(self: *Self, storage: *Storage) !std.fs.File {
};
defer dir.close();
- var file_name: [id_size+4]u8 = undefined;
+ var file_name: [id_size + 4]u8 = undefined;
@memcpy(file_name[0..id_size], self.id);
@memcpy(file_name[id_size..], ".jpg");
@@ -71,3 +80,20 @@ pub fn file(self: *Self, storage: *Storage) !std.fs.File {
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));
+}