add mimetype-based extensions
This commit is contained in:
parent
a44af53a87
commit
96c6f8bc0c
2 changed files with 17 additions and 2 deletions
15
src/main.zig
15
src/main.zig
|
@ -1,15 +1,22 @@
|
|||
const std = @import("std");
|
||||
const http = @import("apple_pie");
|
||||
const hzzp = @import("hzzp");
|
||||
const mimetypes = @import("mimetypes");
|
||||
|
||||
const images_dir_path = "./images";
|
||||
|
||||
var registry: mimetypes.Registry = undefined;
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
std.log.info("welcome to webscale", .{});
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
registry = mimetypes.Registry.init(std.heap.page_allocator);
|
||||
defer registry.deinit();
|
||||
try registry.load();
|
||||
|
||||
// TODO: configurable addr via env var
|
||||
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
|
||||
std.log.info("serving on {}", .{bind_addr});
|
||||
|
@ -219,14 +226,18 @@ fn uploadFile(response: *http.Response, request: http.Request) !void {
|
|||
.{ part.disposition.name, part.disposition.filename, part.content_type, part.body.len },
|
||||
);
|
||||
|
||||
var extensions = registry.getExtensionsByType(part.content_type);
|
||||
if (extensions == null) return error.InvalidContentMimeType;
|
||||
const extension = extensions.?.items[0];
|
||||
|
||||
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 },
|
||||
"{s}/{s}{s}",
|
||||
.{ images_dir_path, image_id, extension },
|
||||
);
|
||||
|
||||
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
||||
|
|
4
zig.mod
4
zig.mod
|
@ -10,3 +10,7 @@ dev_dependencies:
|
|||
- src: git https://github.com/truemedian/hzzp
|
||||
name: hzzp
|
||||
main: src/main.zig
|
||||
|
||||
- src: git https://github.com/frmdstryr/zig-mimetypes
|
||||
name: mimetypes
|
||||
main: mimetypes.zig
|
||||
|
|
Loading…
Reference in a new issue