feature. See also
. The project being documented here (as the example) is the Zig library itself.
Fetch.FileType
const FileType = enum
File
Code
const FileType = enum {
tar,
@"tar.gz",
@"tar.xz",
@"tar.zst",
git_pack,
zip,
fn fromPath(file_path: []const u8) ?FileType {
if (ascii.endsWithIgnoreCase(file_path, ".tar")) return .tar;
if (ascii.endsWithIgnoreCase(file_path, ".tgz")) return .@"tar.gz";
if (ascii.endsWithIgnoreCase(file_path, ".tar.gz")) return .@"tar.gz";
if (ascii.endsWithIgnoreCase(file_path, ".txz")) return .@"tar.xz";
if (ascii.endsWithIgnoreCase(file_path, ".tar.xz")) return .@"tar.xz";
if (ascii.endsWithIgnoreCase(file_path, ".tzst")) return .@"tar.zst";
if (ascii.endsWithIgnoreCase(file_path, ".tar.zst")) return .@"tar.zst";
if (ascii.endsWithIgnoreCase(file_path, ".zip")) return .zip;
if (ascii.endsWithIgnoreCase(file_path, ".jar")) return .zip;
return null;
}
fn fromContentDisposition(cd_header: []const u8) ?FileType {
const attach_end = ascii.findIgnoreCase(cd_header, "attachment;") orelse
return null;
var value_start = ascii.findIgnoreCasePos(cd_header, attach_end + 1, "filename") orelse
return null;
value_start += "filename".len;
if (cd_header[value_start] == '*') {
value_start += 1;
}
if (cd_header[value_start] != '=') return null;
value_start += 1;
var value_end = std.mem.indexOfPos(u8, cd_header, value_start, ";") orelse cd_header.len;
if (cd_header[value_end - 1] == '\"') {
value_end -= 1;
}
return fromPath(cd_header[value_start..value_end]);
}
test fromContentDisposition {
try std.testing.expectEqual(@as(?FileType, .@"tar.gz"), fromContentDisposition("attaChment; FILENAME=\"stuff.tar.gz\"; size=42"));
try std.testing.expectEqual(@as(?FileType, .@"tar.gz"), fromContentDisposition("attachment; filename*=\"stuff.tar.gz\""));
try std.testing.expectEqual(@as(?FileType, .@"tar.xz"), fromContentDisposition("ATTACHMENT; filename=\"stuff.tar.xz\""));
try std.testing.expectEqual(@as(?FileType, .@"tar.xz"), fromContentDisposition("attachment; FileName=\"stuff.tar.xz\""));
try std.testing.expectEqual(@as(?FileType, .@"tar.gz"), fromContentDisposition("attachment; FileName*=UTF-8\'\'xyz%2Fstuff.tar.gz"));
try std.testing.expectEqual(@as(?FileType, .tar), fromContentDisposition("attachment; FileName=\"stuff.tar\""));
try std.testing.expect(fromContentDisposition("attachment FileName=\"stuff.tar.gz\"") == null);
try std.testing.expect(fromContentDisposition("attachment; FileName\"stuff.gz\"") == null);
try std.testing.expect(fromContentDisposition("attachment; size=42") == null);
try std.testing.expect(fromContentDisposition("inline; size=42") == null);
try std.testing.expect(fromContentDisposition("FileName=\"stuff.tar.gz\"; attachment;") == null);
try std.testing.expect(fromContentDisposition("FileName=\"stuff.tar.gz\";") == null);
}
}