Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
new/File.php
$this->getTimeElapsed(),
$this->isDir,
- $this->getMediaType()
+ fn(string $type) => $this->isMediaType($type)
);
}
- private function getMediaType(): string {
- return MediaTypeSniffer::getMediaType('', $this->name);
+ private function isMediaType(string $type): bool {
+ return str_contains(MediaTypeSniffer::isMediaType('', $this->name), $type);
}
new/FileRenderer.php
string $time,
bool $isDir,
- string $mediaType
+ callable $isMediaType
): void;
}
string $time,
bool $isDir,
- string $mediaType
+ callable $isMediaType
): void {
- $iconClass = $this->getIconClass($isDir, $mediaType);
+ $iconClass = $this->getIconClass($isDir, $isMediaType);
$url = '?repo=' . urlencode($this->repoSafeName) . '&hash=' . $sha;
* Maps media types to FontAwesome 6 icon classes.
*/
- private function getIconClass(bool $isDir, string $mime): string {
+ private function getIconClass(bool $isDir, callable $isMediaType): string {
if ($isDir) {
return 'fa-folder';
}
// Explicit Mime Matches
- if ($mime === 'application/pdf') return 'fa-file-pdf';
+ if ($isMediaType('application/pdf')) return 'fa-file-pdf';
// Archives
- if (in_array($mime, [
+ $archives = [
'application/zip', 'application/x-tar', 'application/gzip',
'application/x-bzip2', 'application/vnd.rar', 'application/x-7z-compressed'
- ])) {
- return 'fa-file-archive';
+ ];
+ foreach ($archives as $archive) {
+ if ($isMediaType($archive)) return 'fa-file-archive';
}
// Broad Categories (based on prefix)
- if (str_starts_with($mime, 'image/')) return 'fa-file-image';
- if (str_starts_with($mime, 'audio/')) return 'fa-file-audio';
- if (str_starts_with($mime, 'video/')) return 'fa-file-video';
+ if ($isMediaType('image/')) return 'fa-file-image';
+ if ($isMediaType('audio/')) return 'fa-file-audio';
+ if ($isMediaType('video/')) return 'fa-file-video';
// Code / Text
- if (str_starts_with($mime, 'text/')) return 'fa-file-code';
+ if ($isMediaType('text/')) return 'fa-file-code';
// Common Application Types behaving like code/text
if (
- str_contains($mime, 'javascript') ||
- str_contains($mime, 'json') ||
- str_contains($mime, 'xml') ||
- str_contains($mime, 'php') ||
- str_contains($mime, 'sh')
+ $isMediaType('javascript') ||
+ $isMediaType('json') ||
+ $isMediaType('xml') ||
+ $isMediaType('php') ||
+ $isMediaType('sh')
) {
return 'fa-file-code';
new/MediaTypeSniffer.php
];
- /**
- * Sniffs the media type based on magic bytes (the first few bytes)
- * of the data.
- *
- * @param string $data The raw binary data (a string of bytes).
- * @return string The determined media type (MIME type).
- */
private static function sniff( $data ): string {
$mediaType = 'application/octet-stream';
}
- // Updated to destructure the list of arrays [pattern, type]
foreach( self::FORMATS as [$pattern, $type] ) {
$patternLength = count( $pattern );
}
- /**
- * Determines the media type based purely on the file extension.
- *
- * @param string $filePath The path to the file.
- * @return string The determined media type (MIME type).
- */
private static function getMediaTypeByExtension( $filePath ): string {
$extension = strtolower( pathinfo( $filePath, PATHINFO_EXTENSION ) );
return self::EXTENSION_MAP[$extension] ?? 'application/octet-stream';
}
- /**
- * Public method to get the media type, prioritizing byte analysis and
- * falling back to extension.
- *
- * @param string $data The raw binary data (file content).
- * @param string $filePath The file path (used for extension fallback).
- * @return string The determined media type (MIME type).
- */
- public static function getMediaType( $data, $filePath = '' ): string {
+ public static function isMediaType( $data, $filePath = '' ): string {
$sniffed = self::sniff( $data );

Encapsulates file stuff

Author Dave Jarvis <email>
Date 2026-02-08 19:31:08 GMT-0800
Commit d39aa0017280ed1c94582770bf5d472fb0c875ee
Parent e7fac5f
Delta 22 lines added, 43 lines removed, 21-line decrease