From a9246bde39b6041ef5f7a8ca490de5e1b4745b54 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 28 Jan 2022 21:48:43 -0800 Subject: [PATCH] readme importing, lay groundwork for #6 --- src/db/Version.zig | 4 +++- src/handler/_internal.zig | 10 ++++++++++ src/handler/do_import.zig | 3 ++- src/handler/hook.zig | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/db/Version.zig b/src/db/Version.zig index 3163c8c..35e9646 100644 --- a/src/db/Version.zig +++ b/src/db/Version.zig @@ -30,10 +30,11 @@ deps: DepList, // TODO remove this column, no longer used in upstream Zigmod dev_deps: DepList, root_deps: DepList, build_deps: DepList, +readme: string, pub const table_name = "versions"; -pub fn create(alloc: std.mem.Allocator, pkg: Package, commit: string, unpackedsize: u64, totalsize: u64, files: []const string, tarsize: u64, tarhash: string, deps: []const zigmod.Dep, rootdeps: []const zigmod.Dep, builddeps: []const zigmod.Dep) !Version { +pub fn create(alloc: std.mem.Allocator, pkg: Package, commit: string, unpackedsize: u64, totalsize: u64, files: []const string, tarsize: u64, tarhash: string, deps: []const zigmod.Dep, rootdeps: []const zigmod.Dep, builddeps: []const zigmod.Dep, readme: string) !Version { db.mutex.lock(); defer db.mutex.unlock(); @@ -54,6 +55,7 @@ pub fn create(alloc: std.mem.Allocator, pkg: Package, commit: string, unpackedsi .dev_deps = DepList{ .data = &.{} }, .root_deps = DepList{ .data = rootdeps }, .build_deps = DepList{ .data = builddeps }, + .readme = readme, }); } diff --git a/src/handler/_internal.zig b/src/handler/_internal.zig index 24d655b..d663c33 100644 --- a/src/handler/_internal.zig +++ b/src/handler/_internal.zig @@ -213,3 +213,13 @@ pub fn redirectTo(response: *http.Response, dest: string) !void { try response.headers.put("Location", dest); try response.writeHeader(.found); } + +pub fn readFileContents(dir: std.fs.Dir, alloc: std.mem.Allocator, path: string) !?string { + const file = dir.openFile(path, .{}) catch |err| switch (err) { + error.FileNotFound => return null, + error.IsDir => return null, + else => |e| return e, + }; + defer file.close(); + return try file.reader().readAllAlloc(alloc, 1024 * 1024 * 2); // 2mb +} diff --git a/src/handler/do_import.zig b/src/handler/do_import.zig index 4a4d508..38093e5 100644 --- a/src/handler/do_import.zig +++ b/src/handler/do_import.zig @@ -93,9 +93,10 @@ pub fn get(_: void, response: *http.Response, request: http.Request, args: struc try std.fs.cwd().deleteTree(path); const tarsize = try extras.fileSize(std.fs.cwd(), destpath); const tarhash = try extras.hashFile(alloc, std.fs.cwd(), destpath, .sha256); + const readme = (_internal.readFileContents(dir, alloc, "README.md") catch null) orelse ""; var p = try db.Package.create(alloc, u, name, r, details.id, repo, desc, license, details.star_count); - var v = try db.Version.create(alloc, p, commit, unpackedsize, totalsize, filelist, tarsize, tarhash, deps, rootdeps, builddeps); + var v = try db.Version.create(alloc, p, commit, unpackedsize, totalsize, filelist, tarsize, tarhash, deps, rootdeps, builddeps, readme); // diff --git a/src/handler/hook.zig b/src/handler/hook.zig index f9deb2d..a49a30e 100644 --- a/src/handler/hook.zig +++ b/src/handler/hook.zig @@ -111,8 +111,9 @@ pub fn post(_: void, response: *http.Response, request: http.Request, args: stru try std.fs.cwd().deleteTree(path); const tarsize = try extras.fileSize(std.fs.cwd(), destpath); const tarhash = try extras.hashFile(alloc, std.fs.cwd(), destpath, .sha256); + const readme = (_internal.readFileContents(dir, alloc, "README.md") catch null) orelse ""; - var v = try db.Version.create(alloc, p, commit, unpackedsize, totalsize, filelist, tarsize, tarhash, deps, rootdeps, builddeps); + var v = try db.Version.create(alloc, p, commit, unpackedsize, totalsize, filelist, tarsize, tarhash, deps, rootdeps, builddeps, readme); try p.update(alloc, .license, modfile.yaml.get_string("license")); try p.update(alloc, .description, modfile.yaml.get_string("description")); try p.update(alloc, .star_count, details.star_count);