Files
file-organizer/src/classify.zig
T

65 lines
1.9 KiB
Zig

const std = @import("std");
pub const Category = enum {
image,
document,
music,
video,
archive,
code,
other,
};
const CategoryEntry = struct {
category: Category,
extensions: []const []const u8,
};
const CategoryMap = [_]CategoryEntry{
.{ .category = .image, .extensions = &ImageTypes },
.{ .category = .document, .extensions = &DocumentTypes },
.{ .category = .music, .extensions = &MusicTypes },
.{ .category = .video, .extensions = &VideoTypes },
.{ .category = .archive, .extensions = &ArchiveTypes },
};
const ImageTypes = [_][]const u8{
".arw", ".avif", ".bmp", ".cr2", ".cr3", ".dng", ".gif",
".heic", ".heif", ".ico", ".jpeg", ".jpg", ".nef", ".nrw",
".orf", ".pef", ".png", ".raf", ".raw", ".rw2", ".srw",
".svg", ".tiff", ".tif", ".webp",
};
const DocumentTypes = [_][]const u8{
".csv", ".doc", ".docx", ".epub", ".md", ".odt",
".pdf", ".ppt", ".pptx", ".rtf", ".txt", ".xls", ".xlsx",
};
const MusicTypes = [_][]const u8{
".aac", ".aiff", ".aif", ".alac", ".ape", ".flac",
".m4a", ".mid", ".midi", ".mp3", ".ogg", ".opus",
".wav", ".wma", ".wv",
};
const VideoTypes = [_][]const u8{
".3gp", ".avi", ".flv", ".m2ts", ".m4v", ".mkv",
".mov", ".mp4", ".mts", ".ogv", ".ts", ".vob",
".webm", ".wmv",
};
const ArchiveTypes = [_][]const u8{
".zip", ".rar", ".iso", ".tar", ".gz", ".7z", ".apk", ".cab", ".dar", ".dmg", ".jar", ".pea", ".tar.gz", ".tgz", ".tar.Z", ".tar.bz2", ".tbz2", ".tar.lz", ".tlz", ".tar.xz", ".txz", ".tar.zst",".war", ".zipx",
};
pub fn classify(name: []const u8) Category {
const extension = std.fs.path.extension(name);
for (CategoryMap) |entry| {
for (entry.extensions) |ext| {
const eicDocument = std.ascii.eqlIgnoreCase(extension, ext);
if (eicDocument) {
return entry.category;
}
}
}
return .other;
}