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){
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("-m <ID> Download metadata for ID image.") catch unreachable,
clap.parseParam("-d <ID> Download image data of ID.") catch unreachable,
clap.parseParam("-i <ID> Operate on 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 {
@ -127,11 +129,23 @@ pub fn main() anyerror!void {
_ = curl.curl_easy_setopt(handle, curl.CURLOPT_ERRORBUFFER, &curlerr);
if (args.option("-m")) |id_str| {
const id = std.fmt.parseInt(u64, id_str, 10) catch {
const maybe_id: ?u64 = if (args.option("-i")) |id_str| blk: {
break :blk std.fmt.parseInt(u64, id_str, 10) catch {
log.err("Image ID must be a positive integer.", .{});
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(
bool,
"SELECT true FROM image WHERE id = ?",
@ -173,9 +187,14 @@ pub fn main() anyerror!void {
}
}
if (args.option("-d")) |id_str| {
const id = std.fmt.parseInt(u64, id_str, 10) catch {
log.err("Image ID must be a positive integer.", .{});
if (args.flag("-d")) {
const id = if (maybe_id) |id|
id
else {
log.err(
"Operation download image requires an ID (-i) argument.",
.{},
);
return;
};
const foobar = db.oneAlloc(
@ -199,7 +218,7 @@ pub fn main() anyerror!void {
db.exec(
"UPDATE OR ROLLBACK image SET image = ? WHERE id = ?",
.{
.image = sqlite.Blob{ .data = response_buffer.items },
.image = response_buffer.items,
.id = id,
},
) catch {
@ -211,16 +230,78 @@ pub fn main() anyerror!void {
"UPDATE OR ROLLBACK image SET hash_full = ? WHERE id = ?",
.{ hash_buf2[0..], id },
) 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;
};
try db.exec("COMMIT", .{});
}
} else {
log.err("No metadata for id {d} available.", .{id});
log.err("No metadata for id {d} available", .{id});
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 {