add mimetype-based extensions
This commit is contained in:
parent
a44af53a87
commit
96c6f8bc0c
15
src/main.zig
15
src/main.zig
|
@ -1,15 +1,22 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const http = @import("apple_pie");
|
const http = @import("apple_pie");
|
||||||
const hzzp = @import("hzzp");
|
const hzzp = @import("hzzp");
|
||||||
|
const mimetypes = @import("mimetypes");
|
||||||
|
|
||||||
const images_dir_path = "./images";
|
const images_dir_path = "./images";
|
||||||
|
|
||||||
|
var registry: mimetypes.Registry = undefined;
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
std.log.info("welcome to webscale", .{});
|
std.log.info("welcome to webscale", .{});
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
|
|
||||||
|
registry = mimetypes.Registry.init(std.heap.page_allocator);
|
||||||
|
defer registry.deinit();
|
||||||
|
try registry.load();
|
||||||
|
|
||||||
// TODO: configurable addr via env var
|
// TODO: configurable addr via env var
|
||||||
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
|
const bind_addr = try std.net.Address.parseIp("0.0.0.0", 8080);
|
||||||
std.log.info("serving on {}", .{bind_addr});
|
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 },
|
.{ 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;
|
var image_id_buffer: [256]u8 = undefined;
|
||||||
const image_id = generateImageId(&image_id_buffer);
|
const image_id = generateImageId(&image_id_buffer);
|
||||||
|
|
||||||
var image_path_buffer: [512]u8 = undefined;
|
var image_path_buffer: [512]u8 = undefined;
|
||||||
const image_path = try std.fmt.bufPrint(
|
const image_path = try std.fmt.bufPrint(
|
||||||
&image_path_buffer,
|
&image_path_buffer,
|
||||||
"{s}/{s}.jpg",
|
"{s}/{s}{s}",
|
||||||
.{ images_dir_path, image_id },
|
.{ images_dir_path, image_id, extension },
|
||||||
);
|
);
|
||||||
|
|
||||||
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
const image_file = try std.fs.cwd().createFile(image_path, .{});
|
||||||
|
|
Loading…
Reference in New Issue