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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#!/usr/bin/env php
<?php
// Hubzilla thumbnails storage convertor
function usage() {
echo <<< EOT
Hubzilla thumbnails storage convertor
Usage:
util/storageconv stats # show current stats
util/storageconv fs # move thumbnails from SQL to filesystem
util/storageconv db # move thumbnails from filesystem to SQL
EOT;
}
require_once('include/cli_startup.php');
cli_startup();
use Zotlabs\Lib\Hashpath;
if($argc == 1) {
usage();
killme();
}
if($argc == 2) {
$storage = (intval(get_config('system','photo_storage_type', 1)) > 0 ? 1 : 0);
echo 'Current storage set to: ' . ($storage ? 'filesystem' : 'SQL database') . PHP_EOL;
switch($argv[1]) {
case 'stats':
$x = q("SELECT COUNT(resource_id) AS qty FROM photo WHERE photo_usage = 0 AND os_storage = 1 AND imgscale = 0");
echo 'Local images: ' . $x[0]['qty'] . PHP_EOL;
$x = q("SELECT COUNT(resource_id) AS qty FROM photo WHERE photo_usage = 0 AND imgscale > 0");
echo 'Image thumbnails: ' . $x[0]['qty'] . PHP_EOL;
$xx = intval($x[0]['qty']);
$x = q("SELECT COUNT(resource_id) AS qty FROM photo WHERE photo_usage IN (1, 2)");
echo 'Imported profiles thumbnails: ' . $x[0]['qty'] . PHP_EOL;
$xx += intval($x[0]['qty']);
echo 'Thumbnails total: ' . $xx . PHP_EOL;
$x = q("SELECT COUNT(id) AS qty FROM photo WHERE os_storage != %d AND imgscale > 0",
$storage
);
echo 'Thumbnails to convert: ' . $x[0]['qty'] . PHP_EOL;
break;
case 'fs':
if($storage == 0) {
echo 'Please set system.photo_storage_type to 1 before move thumbnails to filesystem storage' . PHP_EOL;
break;
}
$cur_id = 0;
$i = 0;
$r = dbq("SELECT COUNT(id) AS max_num FROM photo WHERE os_storage = 0 AND imgscale > 0");
$max_num = $r[0]['max_num'];
while ($i < $max_num) {
$x = q("SELECT id, uid, resource_id, content, imgscale FROM photo WHERE id > %d AND os_storage = 0 AND imgscale > 0 ORDER BY id LIMIT 10",
intval($cur_id)
);
if($x) {
foreach($x as $xx) {
if ($xx['uid'] == 0 || in_array($xx['imgscale'], [4, 5, 6]))
$filename = Hashpath::path($xx['resource_id'], 'store/[data]/[xchan]', 2, 1) . '-' . $xx['imgscale'];
else {
$z = q("SELECT channel_address FROM channel WHERE channel_id = %d",
intval($xx['uid'])
);
$filename = 'store/' . $z[0]['channel_address'] . '/' . $xx['resource_id'] . '-' . $xx['imgscale'];
}
if(! file_put_contents($filename, dbunescbin($xx['content']))) {
echo PHP_EOL . 'Failed to save file ' . $filename . PHP_EOL;
continue;
}
$z = q("UPDATE photo SET content = '%s', os_storage = 1 WHERE id = %d",
dbescbin($filename),
intval($xx['id'])
);
if(! $z) {
@unlink($filename);
echo PHP_EOL . 'Failed to update metadata for saved file ' . $filename . PHP_EOL;
}
$cur_id = $xx['id'];
echo '.';
$i++;
}
}
echo ($i % 100 == 0 ? $i : '');
}
echo $i . PHP_EOL . 'Total thumbnails processed: ' . $i;
break;
case 'db':
if($storage == 1) {
echo 'Please set system.photo_storage_type to 0 before move thumbnails to SQL database storage' . PHP_EOL;
break;
}
$cur_id = 0;
$i = 0;
$r = dbq("SELECT COUNT(id) AS max_num FROM photo WHERE os_storage = 1 AND imgscale > 0");
$max_num = $r[0]['max_num'];
while ($i < $max_num) {
$x = q("SELECT id, uid, resource_id, content, imgscale FROM photo WHERE id > %d AND os_storage = 1 AND imgscale > 0 ORDER BY id LIMIT 10",
intval($cur_id)
);
if($x) {
foreach($x as $xx) {
$filename = dbunescbin($xx['content']);
$content = file_get_contents($filename);
if($content) {
$z = q("UPDATE photo SET content = '%s', os_storage = 0 WHERE id = %d",
dbescbin($content),
intval($xx['id'])
);
if(! $z) {
echo PHP_EOL . 'Failed to update stored file metadata ' . $filename . PHP_EOL;
continue;
}
@unlink($filename);
}
else
echo PHP_EOL . 'Can not read file contents ' . $filename . PHP_EOL;
$cur_id = $xx['id'];
echo '.';
$i++;
}
}
echo ($i % 100 == 0 ? $i : '');
}
echo $i . PHP_EOL . 'Total files processed: ' . $i;
break;
default:
usage();
return;
}
echo PHP_EOL;
}
|