forked from ding2/ting_covers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting_covers.pages.inc
105 lines (92 loc) · 3.95 KB
/
ting_covers.pages.inc
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
<?php
/**
* Return the URL for the cover of an object
*/
function ting_covers_objects() {
$covers = array();
$missing_images = $missing_images_local_ids = array();
foreach ($_POST['coverData'] as $cover_info) {
//Reverse cover_info before and after explode to make sure that we explode around
//the last :. Local ids could contain the delimiter!
list($local_id, $image_style) = array_reverse(array_map('strrev', explode(':', strrev($cover_info), 2)));
//Determine if the local id is a known negative
if (cache_get('ting_covers:' . $local_id, FALSE)) {
break;
}
$path = ting_covers_object_path($local_id);
if (file_exists($path)) {
//If we we already have a valid cover image w/o style then just use it
$covers[$local_id.':'.$image_style] = image_style_url($image_style, $path);
} else {
//Mark the image for retrieval;
$missing_images_local_ids[] = $local_id;
if (!isset($missing_images['i_' . $local_id])) {
$missing_images['i_' . $local_id] = array();
}
$missing_images['i_' . $local_id][$image_style] = TRUE;
}
}
//Try to download the missing images
try {
$service = new AdditionalInformationService(variable_get('addi_wsdl_url'), variable_get('addi_username'), variable_get('addi_group'), variable_get('addi_password'));
//Local ids = Faust numbers. Library object identifiers can be confusing...
$additional_informations = $service->getByFaustNumber($missing_images_local_ids);
foreach($missing_images_local_ids as $local_id) {
//Try to extract the image url from the result
$source_url = FALSE;
if (isset($additional_informations[$local_id]) && $ai = $additional_informations[$local_id]) {
if ($ai->detailUrl) {
$source_url = $ai->detailUrl;
} elseif ($ai->thumbnailUrl) {
$source_url = $ai->thumbnailUrl;
}
}
//No cover image found? Cache this for future reference to avoid unnecessary requests
if (!$source_url) {
// foreach ($missing_images['i_' . $local_id] as $image_style => $holder) {
// $covers[$local_id.':'.$image_style] = image_style_url($image_style, 'public://manual/DEFAULT_COVER_IMAGE.jpg');
// }
cache_set('ting_covers:'.$local_id, 1, 'cache', $_SERVER['REQUEST_TIME'] + TING_COVERS_CACHE_LIFETIME);
continue;
}
//Try to download the image locally
if ($file = _ting_covers_pages_fetch_image(ting_covers_object_path($local_id), $source_url)) {
//Generate a path corresponding to the downloaded image, styled
foreach ($missing_images['i_' . $local_id] as $image_style => $holder) {
$covers[$local_id.':'.$image_style] = image_style_url($image_style, $file->uri);
}
}
}
} catch (Exception $e) {
watchdog('ting_covers', 'Unable to retrieve covers from ADDI: %message', array('%message' => $e->getMessage()), WATCHDOG_ERROR);
}
//Return all image information
drupal_json_output($covers);
}
/**
* Helper function to fetch and save a cover image file.
*
* @see image_style_create_derivative()
*
* @param string $filename
* File name, including its path within Drupal's file folder.
* @param string $image_url
* URL for the source image file.
* @return mixed
* A file object or FALSE on error.
*/
function _ting_covers_pages_fetch_image($filename, $image_url) {
$result = drupal_http_request($image_url);
//Bail if the HTTP request failed
if ($result->code != 200) {
return FALSE;
}
// Get the folder for the final location of this preset...
$directory = dirname($filename);
// Build the destination folder tree if it doesn't already exist.
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
watchdog('ting_covers', 'Failed to create directory: %directory', array('%directory' => $directory), WATCHDOG_ERROR);
return FALSE;
}
return file_save_data($result->data, $filename, FILE_EXISTS_REPLACE);
}