Skip to content

Commit

Permalink
readme importing, lay groundwork for #6
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jan 29, 2022
1 parent a322fb2 commit a9246bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/db/Version.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/handler/_internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion src/handler/do_import.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//

Expand Down
3 changes: 2 additions & 1 deletion src/handler/hook.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a9246bd

Please sign in to comment.