-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-new-name.php
More file actions
180 lines (152 loc) · 5.74 KB
/
set-new-name.php
File metadata and controls
180 lines (152 loc) · 5.74 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
use WebFetchFace\DbConnection;
use WebFetchFace\DownloadStatus;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'include' . DIRECTORY_SEPARATOR . 'functions.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'autoloader.php';
// TODO: refactor
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$tmpDir = __DIR__ . DIRECTORY_SEPARATOR . $relDir;
$db = new DbConnection($filesDb);
$result = $db->query('SELECT Id,FileName,MetadataFileName,FileNameConverted,FilePath,ThumbFileName,UrlDomain,DisplayId FROM files WHERE TinyFileName is NULL AND FileStatus=100 ORDER BY Id DESC');
$prepNewThumbnail = $db->prepare(
'UPDATE files SET ThumbFileName=? WHERE Id=?'
);
foreach ($result as $row) {
$id = $row['Id'];
$displayId = $row['DisplayId'];
$filePath = $row['FilePath'];
$fileName = !empty($row['FileNameConverted']) ? $row['FileNameConverted'] : $row['FileName'];
$fpn = $filePath . DIRECTORY_SEPARATOR . $fileName;
$host = $row['UrlDomain'];
$thumbFileName = $row['ThumbFileName'];
$uploadDir = $moveToDir = $relDir . DIRECTORY_SEPARATOR . $host;
if (empty($thumbFileName) && file_exists($row['MetadataFileName'])) {
$jsonData = getJsonFile($row['MetadataFileName']);
}
if (!empty($thumbFileName) && file_exists($thumbFileName)) { // create from thumbfile
$newTTN = updateTinyThumbnail(
$db, $id,
$thumbFileName, $thumbnailWidth,
$thumbnailWidth, $uploadDir, $moveToDir, $prefix = '_tiny'
);
echo "New thumbnail: ", $newTTN, "\n";
} else if (file_exists($fpn) && !is_dir($fpn)) {
$dir = $relDir . DIRECTORY_SEPARATOR . $host;
$aspectRatio = getAspectRatio($ffprobe, $id,$fpn,$dir);
$bigThumbnailHeight = floor( $bigThumbnailWidth / $aspectRatio);
$duration = getDuration($ffprobe, $id,$fpn,$dir);
$remainingSeconds = $duration - $startSeconds;
if ($remainingSeconds > $endSeconds) {
$remainingSeconds -= $endSeconds;
}
$newThumbName = $dir . DIRECTORY_SEPARATOR . getThumbName($id, $displayId, '_generated_ffmpeg', true);
$command = getResizeCommand($ffmpeg, $startSeconds, $remainingSeconds, $fpn, $bigThumbnailWidth, $bigThumbnailHeight, $newThumbName);
echo $command,"\n";
exec($command);
if (file_exists($newThumbName)) {
$prepNewThumbnail->execute(
array(
$newThumbName, $id
)
);
$newTTN = updateTinyThumbnail(
$db, $id,
basename($newThumbName), $thumbnailWidth,
$thumbnailWidth, dirname($newThumbName), $moveToDir, $prefix = '_tiny'
);
}
} else {
echo "No such file: ", $fpn , "\n";
}
}
$result = $db->query('SELECT Id,Title,FileName,FilePath,MetadataFileName,DisplayId FROM files WHERE FileNameConverted IS NULL AND DownloadedAt IS NOT NULL AND FileStatus=100 ORDER BY Id DESC');
$changedFiles = 0;
$toDownload = array();
$prepConverted = $db->prepare('UPDATE files SET FileNameConverted=? WHERE Id=?');
foreach ($result as $row) {
$id = $row['Id'];
$filepath = $row['FilePath'];
$filenameOld = $row['FileName'];
$displayId = $row['DisplayId'];
$parts = getFilenameParts($filenameOld);
$filenameCandidate = preg_replace('~/+~', '/', $filepath . DIRECTORY_SEPARATOR . $parts[0]);
$filePathOld = preg_replace('~/+~', '/', $filepath . DIRECTORY_SEPARATOR . $filenameOld);
$path1 = glob($filenameCandidate .'*');
$path2 = glob(preg_replace('~/+~', '/', $filepath . '/*__' . $id . '.*'));
$path3 = glob(preg_replace('~/+~', '/', $filepath . '/*' . $displayId . '*'));
$path = array_merge($path1, $path2, $path3);
if (!count($path)) {
continue;
}
// var_dump($path);exit;
$filename = '';
foreach ($path as $foundFile) {
if (preg_match('/\.(jpg|jpeg|mp3|json|m4a)$/i',$foundFile)) {
continue;
}
$filename = $foundFile;
}
if (!$filename || is_dir($filename)) {
echo "no candidates: $filePathOld\n";
continue;
}
/*
if ($filePathOld != $filename) {
echo $filePathOld , "\n",$filename , "\n\n";
}
*/
if($row['DisplayId']) {
$did = $row['DisplayId'];
}
if ($row['MetadataFileName']) {
$data = getJsonFile($row['MetadataFileName']);
if (!$did) {
$did = getDisplayId($data);
}
} else {
echo "No displayId, no metadata: $id\n";
continue;
}
$did .= '__' . $id;
$newFilename = getSanitizedName($did,$row['Title'],$filename);
$newFilenamePath = preg_replace('~/+~', '/',$filepath . DIRECTORY_SEPARATOR . $newFilename);
if ($filename !== $newFilenamePath) {
if(!is_dir($filename) && rename($filename,$newFilenamePath)) {
$prepConverted->execute(array($newFilename,$id));
echo "rename: $filename -> $newFilenamePath\n";
} else {
echo "cannot update $id\n";
}
} else {
$prepConverted->execute(array($newFilename,$id));
}
}
$result = $db->query('SELECT Id,Title,FileNameConverted,FilePath,MetadataFileName,DisplayId FROM files'
. ' WHERE FilePath ="files/" AND FileNameConverted IS NOT NULL AND FileStatus=100 ORDER BY Id DESC');
$dirs = getDirs();
$prepFilepath = $db->prepare('UPDATE files SET FilePath=? WHERE Id=?');
foreach ($result as $row) {
$id = $row['Id'];
$filepath = $row['FilePath'];
$filename = $row['FileNameConverted'];
$lcfn = strtolower($filename);
foreach ($dirs as $match => $dir) {
if (strpos($lcfn, $match) !== false) {
$newdir = preg_replace('~/+~', '/',$filepath . DIRECTORY_SEPARATOR
. 'pohadky' . DIRECTORY_SEPARATOR . 'rpi' . DIRECTORY_SEPARATOR . $dir);
if (is_dir($newdir)) {
$newfilename = str_replace('_-_','-', preg_replace('~/+~', '/',$newdir . DIRECTORY_SEPARATOR . $filename));
$oldfilename = preg_replace('~/+~', '/',$filepath . DIRECTORY_SEPARATOR . $filename);
if (!is_dir($filename) && rename($oldfilename,$newfilename)) {
$prepFilepath->execute(array($newdir,$id));
} else if (file_exists($newfilename)) {
$prepFilepath->execute(array($newfilename, $id));
echo "found in new location\n";
}
}
continue;
}
}
}