-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmedia.php
More file actions
70 lines (66 loc) · 2.13 KB
/
Copy pathmedia.php
File metadata and controls
70 lines (66 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Where media lives — the single place the public pages and the admin agree on.
*
* The database stores bare filenames ('ana_margolin_1.m4a'), never paths, so
* moving to object storage needs no schema change and no data migration. Only
* the URL prefix moves.
*
* Default is local: /images/... and /readings/... served off this host,
* exactly as before. Create /home/raphi/config/media_config.php returning
*
* <?php return ['media_url' => 'https://media.yiddish.nu'];
*
* and every page starts serving from the bucket instead. Delete it to roll
* back. That one file is the whole switch.
*/
if (!isset($MEDIA_BASE)) {
$MEDIA_BASE = '';
$__mcfg = '/home/raphi/config/media_config.php';
if (is_file($__mcfg)) {
$__m = require $__mcfg;
$MEDIA_BASE = rtrim((string)($__m['media_url'] ?? ''), '/');
}
}
if (!function_exists('media_url')) {
/**
* @param string $file bare filename from the database
* @param string $kind 'images' or 'readings'
*/
function media_url(?string $file, string $kind): string
{
global $MEDIA_BASE;
$file = trim((string)$file);
if ($file === '') {
$file = $kind === 'images' ? 'default.png' : '';
}
// Legacy rows might already hold a full URL; leave those alone.
if (preg_match('#^https?://#i', $file)) {
return $file;
}
return $MEDIA_BASE . '/' . $kind . '/' . rawurlencode($file);
}
}
if (!function_exists('audio_mime')) {
function audio_mime(string $file): string
{
return match (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
'm4a' => 'audio/mp4',
'wav' => 'audio/wav',
'ogg' => 'audio/ogg',
'flac' => 'audio/flac',
default => 'audio/mpeg',
};
}
}
if (!function_exists('image_mime')) {
function image_mime(string $file): string
{
return match (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
default => 'image/jpeg',
};
}
}