Move path splitting to controller

This commit is contained in:
jaina heartles 2022-12-15 06:29:56 -08:00
parent a6b928b42b
commit 7d362f0708
2 changed files with 9 additions and 7 deletions

View file

@ -781,13 +781,9 @@ fn ApiConn(comptime DbConn: type) type {
return try self.backendDriveEntryToFrontend(entry, true);
}
pub fn driveMkdir(self: *Self, path: []const u8) !DriveEntry {
pub fn driveMkdir(self: *Self, parent_path: []const u8, name: []const u8) !DriveEntry {
const user_id = self.user_id orelse return error.NoToken;
var split = std.mem.splitBackwards(u8, path, "/");
std.log.debug("{s}", .{path});
const base = split.first();
const dir = split.rest();
const entry = try services.drive.create(self.db, user_id, dir, base, null, self.allocator);
const entry = try services.drive.create(self.db, user_id, parent_path, name, null, self.allocator);
errdefer util.deepFree(self.allocator, entry);
return try self.backendDriveEntryToFrontend(entry, true);
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const api = @import("api");
const http = @import("http");
const util = @import("util");
@ -68,7 +69,12 @@ pub const mkdir = struct {
pub const Args = DriveArgs;
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const result = try srv.driveMkdir(req.args.path);
var split = std.mem.splitBackwards(u8, std.mem.trim(u8, req.args.path, "/"), "/");
const name = split.first();
const parent = split.rest();
std.log.debug("{s}, {s}", .{ parent, name });
const result = try srv.driveMkdir(parent, name);
errdefer util.deepFree(srv.allocator, result);
try res.json(.created, result);