~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
44 lines · 2.0 KB Raw History
const std = @import("std");
const http_api = @import("../../http/api.zig");
const entries = @import("web_drawings").entries;
pub const slots = @import("icons/slots.zig");

/// The inline script `decorate` adds, which pages list in their script policy.
pub const script = blk: {
    var drawings: []const u8 = "";
    for (entries, 0..) |entry, i| {
        drawings = drawings ++ (if (i > 0) "," else "") ++ std.fmt.comptimePrint("{f}:{f}", .{ std.json.fmt(entry.slot, .{}), std.json.fmt("/ui/icon?cid=" ++ entry.cid, .{}) });
    }
    break :blk "(()=>{const drawings={" ++ drawings ++ "};\n" ++ @embedFile("icons/replace.js") ++ "})();";
};

/// Whether this build carries operator drawings.
pub const has_drawings = entries.len != 0;

/// The inline style `decorate` adds.
pub const style = @embedFile("icons/style.css");

pub fn decorate(allocator: std.mem.Allocator, html: []const u8) ![]const u8 {
    if (entries.len == 0) return html;
    const position = std.mem.lastIndexOf(u8, html, "</body>") orelse return error.MissingPageBody;
    var out: std.Io.Writer.Allocating = .init(allocator);
    try out.writer.writeAll(html[0..position]);
    try out.writer.writeAll("<style>" ++ style ++ "</style><script>" ++ script ++ "</script>");
    try out.writer.writeAll(html[position..]);
    return out.toOwnedSlice();
}

pub fn serve(request: *http_api.Request) !void {
    var buffer: [256]u8 = undefined;
    const cid = http_api.queryParam(request.url.raw, "cid", &buffer) orelse "";
    inline for (entries) |entry| {
        if (std.mem.eql(u8, cid, entry.cid)) {
            return http_api.respond(request, .ok, entry.png, &.{
                .{ .name = "content-type", .value = "image/png" },
                .{ .name = "cache-control", .value = "public, max-age=31536000, immutable" },
                .{ .name = "x-content-type-options", .value = "nosniff" },
            });
        }
    }
    try http_api.respond(request, .not_found, "Icon not found", &.{.{ .name = "cache-control", .value = "no-store" }});
}