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

zat.dev/zds

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

0
0
47 lines · 2.1 KB Raw History
//! The garden skin's ground textures: five small CC0 photographs from
//! OpenGameArt (grass, grass2, sand, bark, dirt) that skins/garden/garden.js
//! tiles across its scene. Served from this origin and cached for a year;
//! a texture that ever changes gets a new name.

const std = @import("std");
const http_api = @import("../../http/api.zig");
const garden_built = @import("skin.zig").garden_built;

pub const path = "/web/texture";

const Texture = struct { name: []const u8, jpg: []const u8 };

pub const entries = if (!garden_built) [_]Texture{} else [_]Texture{
    .{ .name = "grass", .jpg = @embedFile("skins/garden/tex/grass.jpg") },
    .{ .name = "grass2", .jpg = @embedFile("skins/garden/tex/grass2.jpg") },
    .{ .name = "sand", .jpg = @embedFile("skins/garden/tex/sand.jpg") },
    .{ .name = "bark", .jpg = @embedFile("skins/garden/tex/bark.jpg") },
    .{ .name = "dirt", .jpg = @embedFile("skins/garden/tex/dirt.jpg") },
};

pub fn find(name: []const u8) ?[]const u8 {
    for (entries) |entry| if (std.mem.eql(u8, name, entry.name)) return entry.jpg;
    return null;
}

pub fn serve(request: *http_api.Request) !void {
    var buffer: [32]u8 = undefined;
    const name = http_api.queryParam(request.url.raw, "name", &buffer) orelse "";
    const jpg = find(name) orelse
        return http_api.respond(request, .not_found, "Texture not found", &.{.{ .name = "cache-control", .value = "no-store" }});
    try http_api.respond(request, .ok, jpg, &.{
        .{ .name = "content-type", .value = "image/jpeg" },
        .{ .name = "cache-control", .value = "public, max-age=31536000, immutable" },
        .{ .name = "x-content-type-options", .value = "nosniff" },
    });
}

test "every garden texture is a jpeg, and nothing else is served" {
    for (entries) |entry| {
        try std.testing.expect(std.mem.startsWith(u8, entry.jpg, "\xff\xd8\xff"));
        try std.testing.expect(entry.jpg.len < 80 * 1024);
    }
    try std.testing.expect((find("grass") != null) == garden_built);
    try std.testing.expect(find("../grass") == null);
    try std.testing.expect(find("") == null);
}