diff options
| author | ilotterytea <iltsu@alright.party> | 2025-06-04 21:02:51 +0400 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-06-04 21:02:51 +0400 |
| commit | d59e9b569fb828cdb145a3497c1b1f9e27cd03ad (patch) | |
| tree | f4f96c76ec2b6cc9a3aa42aba5efbf213b589143 /lib/file.php | |
| parent | 28658d12a464777b50c789c2e9c3f86ce8f07da0 (diff) | |
feat: verify file mime type
Diffstat (limited to 'lib/file.php')
| -rw-r--r-- | lib/file.php | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/file.php b/lib/file.php new file mode 100644 index 0000000..4fcb607 --- /dev/null +++ b/lib/file.php @@ -0,0 +1,37 @@ +<?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php'; + +function verify_mimetype(string $file_path, string $mimetype): bool +{ + $path = escapeshellarg($file_path); + + if (str_starts_with($mimetype, 'image/')) { + $output = shell_exec("identify -quiet -ping $path"); + return !empty($output); + } else if (str_starts_with($mimetype, 'video/') || str_starts_with($mimetype, 'audio/')) { + $output = []; + $exitCode = 0; + $cmd = 'ffprobe -v error -i "/path/to/video.mp4" 2>&1'; + exec($cmd, $output, $exitCode); + return $exitCode === 0; + } + + throw new RuntimeException("Illegal type for MIME verifications: $mimetype"); +} + +function delete_file(string $file_id, string $file_extension): bool +{ + $paths = [ + FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_extension}", + FILE_THUMBNAIL_DIRECTORY . "/{$file_id}.webp", + FILE_METADATA_DIRECTORY . "/{$file_id}.metadata.json" + ]; + + foreach ($paths as $path) { + if (is_file($path) && !unlink($path)) { + return false; + } + } + + return true; +}
\ No newline at end of file |
