use part body when writing to file
This commit is contained in:
parent
1a3c1e354f
commit
a44af53a87
36
src/main.zig
36
src/main.zig
|
@ -214,23 +214,27 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
|
||||||
var hzzp_buffer: [1024]u8 = undefined;
|
var hzzp_buffer: [1024]u8 = undefined;
|
||||||
|
|
||||||
while (try multipart.next(&hzzp_buffer)) |part| {
|
while (try multipart.next(&hzzp_buffer)) |part| {
|
||||||
std.log.info("part: {}", .{part});
|
std.log.info(
|
||||||
|
"got part from multipart request! name='{s}' filename='{s}' content_type='{s}' length={d}",
|
||||||
|
.{ part.disposition.name, part.disposition.filename, part.content_type, part.body.len },
|
||||||
|
);
|
||||||
|
|
||||||
|
var image_id_buffer: [256]u8 = undefined;
|
||||||
|
const image_id = generateImageId(&image_id_buffer);
|
||||||
|
|
||||||
|
var image_path_buffer: [512]u8 = undefined;
|
||||||
|
const image_path = try std.fmt.bufPrint(
|
||||||
|
&image_path_buffer,
|
||||||
|
"{s}/{s}.jpg",
|
||||||
|
.{ images_dir_path, image_id },
|
||||||
|
);
|
||||||
|
|
||||||
|
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
||||||
|
try image_file.writer().writeAll(part.body);
|
||||||
|
|
||||||
|
try response.writer().writeAll(image_path);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var image_id_buffer: [256]u8 = undefined;
|
|
||||||
const image_id = generateImageId(&image_id_buffer);
|
|
||||||
|
|
||||||
var image_path_buffer: [512]u8 = undefined;
|
|
||||||
const image_path = try std.fmt.bufPrint(
|
|
||||||
&image_path_buffer,
|
|
||||||
"{s}/{s}.jpg",
|
|
||||||
.{ images_dir_path, image_id },
|
|
||||||
);
|
|
||||||
|
|
||||||
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
|
||||||
try image_file.writer().writeAll(request.body);
|
|
||||||
|
|
||||||
try response.writer().writeAll(image_path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void {
|
fn fetchFile(response: *http.Response, request: http.Request, filename: []const u8) !void {
|
||||||
|
|
Loading…
Reference in New Issue