frunk
Lonnie 2021-08-01 05:34:39 +02:00
parent f95b8fa310
commit d162e42b1e
1 changed files with 91 additions and 10 deletions

View File

@ -10,8 +10,10 @@ const log = std.log.scoped(.derploader);
const params = [_]clap.Param(clap.Help){ const params = [_]clap.Param(clap.Help){
clap.parseParam("-h, --help Display this help and exit.") catch unreachable, clap.parseParam("-h, --help Display this help and exit.") catch unreachable,
clap.parseParam("-c, --create=<PATH> Create new database at PATH.") catch unreachable, clap.parseParam("-c, --create=<PATH> Create new database at PATH.") catch unreachable,
clap.parseParam("-m <ID> Download metadata for ID image.") catch unreachable, clap.parseParam("-i <ID> Operate on ID.") catch unreachable,
clap.parseParam("-d <ID> Download image data of ID.") catch unreachable, clap.parseParam("-m Download metadata for ID image.") catch unreachable,
clap.parseParam("-d Download image data of ID.") catch unreachable,
clap.parseParam("-e <PATH> Extract image.") catch unreachable,
}; };
fn printFullUsage(w: anytype) !void { fn printFullUsage(w: anytype) !void {
@ -127,11 +129,23 @@ pub fn main() anyerror!void {
_ = curl.curl_easy_setopt(handle, curl.CURLOPT_ERRORBUFFER, &curlerr); _ = curl.curl_easy_setopt(handle, curl.CURLOPT_ERRORBUFFER, &curlerr);
if (args.option("-m")) |id_str| { const maybe_id: ?u64 = if (args.option("-i")) |id_str| blk: {
const id = std.fmt.parseInt(u64, id_str, 10) catch { break :blk std.fmt.parseInt(u64, id_str, 10) catch {
log.err("Image ID must be a positive integer.", .{}); log.err("Image ID must be a positive integer.", .{});
return; return;
}; };
} else null;
if (args.flag("-m")) {
const id = if (maybe_id) |id|
id
else {
log.err(
"Operation download metadata requires an ID (-i) argument.",
.{},
);
return;
};
const foobar = db.one( const foobar = db.one(
bool, bool,
"SELECT true FROM image WHERE id = ?", "SELECT true FROM image WHERE id = ?",
@ -173,9 +187,14 @@ pub fn main() anyerror!void {
} }
} }
if (args.option("-d")) |id_str| { if (args.flag("-d")) {
const id = std.fmt.parseInt(u64, id_str, 10) catch { const id = if (maybe_id) |id|
log.err("Image ID must be a positive integer.", .{}); id
else {
log.err(
"Operation download image requires an ID (-i) argument.",
.{},
);
return; return;
}; };
const foobar = db.oneAlloc( const foobar = db.oneAlloc(
@ -199,7 +218,7 @@ pub fn main() anyerror!void {
db.exec( db.exec(
"UPDATE OR ROLLBACK image SET image = ? WHERE id = ?", "UPDATE OR ROLLBACK image SET image = ? WHERE id = ?",
.{ .{
.image = sqlite.Blob{ .data = response_buffer.items }, .image = response_buffer.items,
.id = id, .id = id,
}, },
) catch { ) catch {
@ -211,16 +230,78 @@ pub fn main() anyerror!void {
"UPDATE OR ROLLBACK image SET hash_full = ? WHERE id = ?", "UPDATE OR ROLLBACK image SET hash_full = ? WHERE id = ?",
.{ hash_buf2[0..], id }, .{ hash_buf2[0..], id },
) catch { ) catch {
sqliteErrorReport("Couldn't insert", &db); sqliteErrorReport("Couldn't add iamge hash", &db);
return;
};
try db.exec("COMMIT", .{});
response_buffer.clearRetainingCapacity();
std.mem.set(u8, hash_buf[0..], 0);
std.mem.set(u8, hash_buf2[0..], 0);
}
if (res.thumb_url) |url| {
easyFetch(handle, url, &response_buffer) catch return;
try db.exec("BEGIN IMMEDIATE;", .{});
errdefer db.exec("ROLLBACK;", .{}) catch {};
db.exec(
"UPDATE OR ROLLBACK image SET thumb = ? WHERE id = ?",
.{
.thumb = response_buffer.items,
.id = id,
},
) catch {
sqliteErrorReport("Couldn't add thumb to DB", &db);
return;
};
try hashit(response_buffer.items);
db.exec(
"UPDATE OR ROLLBACK image SET hash_thumb = ? WHERE id = ?",
.{ hash_buf2[0..], id },
) catch {
sqliteErrorReport("Couldn't add thumb hash", &db);
return; return;
}; };
try db.exec("COMMIT", .{}); try db.exec("COMMIT", .{});
} }
} else { } else {
log.err("No metadata for id {d} available.", .{id}); log.err("No metadata for id {d} available", .{id});
return; return;
} }
} }
if (args.option("-e")) |path| {
const id = if (maybe_id) |id|
id
else {
log.err(
"Operation extract image requires an ID (-i) argument.",
.{},
);
return;
};
const maybe_image = db.oneAlloc(
[]u8,
alloc,
"SELECT image FROM image WHERE id = ?",
.{},
.{ .id = id },
) catch {
sqliteErrorReport("ID check read error", &db);
return;
};
if (maybe_image) |image| {
var file = try std.fs.cwd().createFile(
path,
.{
.read = false,
.truncate = true,
},
);
try file.writeAll(image);
file.close();
} else {
log.info("No image data for ID {d}.", .{id});
}
}
} }
fn easyFetch(handle: *curl.CURL, url: [*:0]const u8, resp: *std.ArrayList(u8)) !void { fn easyFetch(handle: *curl.CURL, url: [*:0]const u8, resp: *std.ArrayList(u8)) !void {