-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_file.module
129 lines (115 loc) · 3.18 KB
/
uuid_file.module
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
<?php
/**
* @file
* UUID Files module file.
*/
/**
* Implements hook_menu().
*/
function uuid_file_menu() {
$items = array();
$items['file-uuid'] = array(
'title' => 'File download',
'page callback' => 'uuid_file_stream_download',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_stream_wrappers().
*/
function uuid_file_stream_wrappers() {
return array(
'uuid' => array(
'name' => t('UUID File Stream'),
'class' => 'UuidFileStreamWrapper',
'description' => t('Local Files which are using their UUID as resource locator.'),
'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
),
);
}
/**
* Menu callback for UUID file stream.
*/
function uuid_file_stream_download($pseudo) {
$pieces = explode('.', $pseudo);
$uuid = array_shift($pieces);
$uri = db_select('file_managed', 'f')
->fields('f', array('uri'))
->condition('f.uuid', $uuid)
->execute()->fetchField();
if ($uri) {
$scheme = file_uri_scheme($uri);
$target = file_uri_target($uri);
$args = explode('/', $target);
array_unshift($args, $scheme);
if (isset($_GET['style']) && ($style = image_style_load($_GET['style']))) {
array_unshift($args, $style);
call_user_func_array('image_style_deliver', $args);
}
else {
call_user_func_array('file_download', $args);
}
}
return MENU_NOT_FOUND;
}
/**
* Returns a file object by a given uri.
*
* @param $uri
* A string containing a valid uri.
*
* @return
* A file entity object, FALSE when there's no file for the given uri.
*/
function uuid_file_by_uri($uri) {
$files = &drupal_static(__FUNCTION__, array());
if (isset($files[$uri])) {
return $files[$uri];
}
if (strpos($uri, '://styles/')) {
// Obviously this is an image style uri.
// We have to manually extract the file's uri.
$scheme = file_uri_scheme($uri);
$target = file_uri_target($uri);
$pieces = explode('/' . $scheme . '/', $target);
// This should be the file target.
$target = !empty($pieces[1]) ? $pieces[1] : $target;
// Try to re-construct the source uri.
$uri = $scheme . '://' . $target;
}
$files[$uri] = FALSE;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'file');
$query->propertyCondition('uri', $uri);
$result = $query->execute();
if (!empty($result['file'])) {
$stub = reset($result['file']);
$files[$uri] = file_load($stub->fid);
}
return $files[$uri];
}
/**
* Returns a possible file extension for a given MIME type.
*
* @param $mime
* A valid MIME type as string.
*
* @return
* When found, a possible file extension as string.
* Otherwise, the return value is an empty string.
*/
function uuid_file_extension_by_mime($mime) {
$mapping = &drupal_static(__FUNCTION__, array());
if (!isset($mapping[$mime])) {
$mapping[$mime] = '';
require_once 'includes/file.mimetypes.inc';
$mimetype_mapping = file_mimetype_mapping();
foreach ($mimetype_mapping['extensions'] as $extension => $mapkey) {
$mapping[$mimetype_mapping['mimetypes'][$mapkey]] = $extension;
}
}
return $mapping[$mime];
}