forked from hugoh/Offlickr2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalStorage.php
334 lines (270 loc) · 9.53 KB
/
LocalStorage.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
require_once('Dialog.php');
abstract class LocalItem {
protected $temporary_dir = false;
protected $data = array();
function __construct($local_storage, $dialog) {
$this->local_storage = $local_storage;
$this->dialog = $dialog;
}
protected function assign_data_value($type, $value) {
$this->data[$type] = $value;
$this->dialog->info(3, "Target $type filename: " . $value);
}
function setup_temporary_dir() {
$path="/tmp";
$prefix="offlickr2.";
// FIXME: Not atomic
$tempname = tempnam($path,$prefix);
if (!$tempname) {
throw new Exception("Could not create temporary directory!");
}
if (!unlink($tempname)) {
throw new Exception("Could not setup temporary directory!");
}
// Create the temporary directory and returns its name.
if (mkdir($tempname)) {
$this->temporary_dir = $tempname;
$this->dialog->info(3, "Created temporary directory $tempname");
} else {
throw new Exception("Could not create temporary directory!");
}
}
protected function get_filename($filename, $temporary) {
if ($temporary == true) {
if ($this->temporary_dir == false) {
throw new Exception("Temporary directory not setup");
}
$dir = $this->temporary_dir;
} else {
$dir = $this->location;
}
return $dir . '/' . $filename;
}
function has_data($type) {
return is_file($this->full_path($this->data[$type]));
}
function get_data_filename($type, $temporary = false) {
return $this->get_filename($this->data[$type], $temporary);
}
private function full_path($path) {
return $this->location . '/' . $path;
}
private function create_target_directory() {
if (!is_dir($this->location)) {
$this->dialog->info(2, "Creating target directory " . $this->location);
if (!mkdir($this->location, $mode = 0755, $recursive = true)) {
throw new Exception("Could not create target directory");
}
}
}
function is_backed_up() {
foreach(array_keys($this->data) as $d) {
if (!$this->has_data($d)) {
$this->dialog->info(3, "Could not find $d file for back up");
return false;
}
}
return true;
}
protected function move_file($label, $old, $new) {
if (!rename($old, $new)) {
throw new Exception("Could not move temporary files for $label");
} else {
$this->dialog->info(3, "Moving $label to " . $new);
}
}
function save_temporary_files() {
$this->create_target_directory();
foreach(array_keys($this->data) as $d) {
if (!is_file($this->get_data_filename($d, true))) {
continue;
}
$this->move_file($d, $this->get_data_filename($d, true), $this->get_data_filename($d));
}
$this->dialog->info(2, "Files moved to $this->location");
}
private function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
function __destruct() {
if ($this->temporary_dir != false) {
$this->dialog->info(3, "Cleaning up $this->temporary_dir");
$this->rrmdir($this->temporary_dir);
}
}
}
class LocalSet extends LocalItem {
const suffix = '.xml';
const target_dir = "sets";
protected $total_pages = 0;
const INFO = 'info';
function __construct($set_id, $local_storage, $dialog) {
parent::__construct($local_storage, $dialog);
$this->location = $local_storage->relative(self::target_dir);
$this->set_id = $set_id;
// Target filenames
$this->assign_data_value(self::INFO, $set_id . self::suffix);
}
function set_pages($pages) {
$this->total_pages = $pages;
}
function get_photoset_photos_filename($page, $temporary = false) {
return $this->get_filename($this->set_id . '-photos-' . $page . self::suffix, $temporary);
}
function save_temporary_files() {
parent::save_temporary_files();
for($page = 1; $page <= $this->total_pages; $page++) {
$this->move_file("photo page $page", $this->get_photoset_photos_filename($page, true), $this->get_photoset_photos_filename($page));
}
}
}
class LocalMedia extends LocalItem {
const metadata_suffix = '-info.xml';
const comments_suffix = '-comments.xml';
const BINARY_FLAG = 1;
const METADATA_FLAG = 2;
const COMMENTS_FLAG = 4;
const ALL_FLAGS = 7;
const METADATA = 'metadata';
const BINARY = 'binary';
const COMMENTS = 'comments';
const PHOTO_TYPE = 0;
const VIDEO_TYPE = 1;
private static $TYPE_STR = array(
self::PHOTO_TYPE => 'photo',
self::VIDEO_TYPE => 'video'
);
function __construct($photo_info, $local_storage, $dialog) {
parent::__construct($local_storage, $dialog);
$this->photo_info = $photo_info['photo'];
// Target directory
$year = substr($this->photo_info['dates']['taken'], 0, 4);
$month = substr($this->photo_info['dates']['taken'], 5, 2);
$day = substr($this->photo_info['dates']['taken'], 8, 2);
$this->location = $local_storage->relative($year . '/' . $month . '/' . $day);
$this->dialog->info(3, "Target directory: $this->location");
// Photo format
if (array_key_exists('video', $this->photo_info)) {
$this->media_type = self::VIDEO_TYPE;
// We don't know the extension ahead of time, so let's call it .video
$extension = 'video';
} else {
$this->media_type = self::PHOTO_TYPE;
if (array_key_exists('originalformat', $this->photo_info)) {
$extension = $this->photo_info['originalformat'];
} else {
// FIXME: assume it's JPEG; a better way to do this is to call flickr.photos.getInfo and look it up
$extension = 'jpg';
}
}
// Target filenames
$this->assign_data_value(self::BINARY, $this->photo_info['id'] . '.' . $extension);
$this->assign_data_value(self::METADATA, $this->photo_info['id'] . self::metadata_suffix);
$this->assign_data_value(self::COMMENTS, $this->photo_info['id'] . self::comments_suffix);
}
function is_video() {
return $this->media_type === self::VIDEO_TYPE;
}
function get_media_type() {
return self::$TYPE_STR[$this->media_type];
}
function set_extension($content_type) {
$parts = explode('/', $content_type);
$extension = $parts[1];
$this->assign_data_value(self::BINARY, $this->photo_info['id'] . '.' . $extension);
}
static function check_backup_dir($dir, &$present, $dialog, &$files = 0, $depth = 1) {
// This assumes that the backup directory is in the right format
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != ".." && ($depth > 1 || $object != LocalSet::target_dir)) {
$fullpath = $dir."/".$object;
if (filetype($fullpath) == "dir") {
LocalMedia::check_backup_dir($dir."/".$object, $present, $dialog, $files, $depth + 1);
} else {
// Check for binary
if (preg_match('/^(\d+)\./', $object, $matches) && filesize($fullpath) > 0) {
$dialog->progress(++$files);
$present[(string)$matches[1]] |= self::BINARY_FLAG;
next;
}
// Check for metadata
if (preg_match('/^(\d+)' . self::metadata_suffix . '/', $object, $matches)) {
$dialog->progress(++$files);
$present[(string)$matches[1]] |= self::METADATA_FLAG;
next;
}
// Check for comments
if (preg_match('/^(\d+)' . self::comments_suffix . '/', $object, $matches)) {
$dialog->progress(++$files);
$present[(string)$matches[1]] |= self::COMMENTS_FLAG;
next;
}
}
}
}
if ($depth == 1) {
$dialog->progress_done(' files scanned');
}
}
return $present;
}
static function backup_list($local_storage) {
$present = array();
LocalMedia::check_backup_dir($local_storage->directory, $present, $local_storage->dialog);
return $present;
}
static function does_photo_seem_backed_up($present, $photo_id) {
return $present[$photo_id] == self::ALL_FLAGS;
}
}
class LocalStorage {
private $photos_backed_up = false;
function __construct($dir, $debug_level) {
$this->dialog = new Dialog($debug_level);
if (! is_string($dir)) {
$this->dialog->error("Local directory not specified");
throw new Exception();
}
$this->directory = $dir;
$this->dialog->info(2, "Checking for local directory $dir");
if (! is_dir($this->directory)) {
$this->dialog->info(1, "Creating $dir");
mkdir($dir, $mode = 0755, $recursive = true);
}
}
private function photo_directory($photo_info) {
print($photo_info['dates']['taken']);
}
function is_photo_present($photo_info) {
$this->photo_directory($photo_info);
}
function local_media_factory($photo_info) {
return new LocalMedia($photo_info, $this, $this->dialog);
}
function local_set_factory($set_id) {
return new LocalSet($set_id, $this, $this->dialog);
}
function relative($path) {
return $this->directory . '/' . $path;
}
function does_photo_seem_backed_up($photo_id) {
if ($this->photos_backed_up === false) {
$this->dialog->info(1, "Parsing local photo backup");
$this->photos_backed_up = LocalMedia::backup_list($this);
}
return LocalMedia::does_photo_seem_backed_up($this->photos_backed_up, $photo_id);
}
}
?>