Permalink
Browse files

feature(files): update file plugin to new file serving API

Updates file plugin to use new elgg_get_download_url() to handle
downloads, and elgg_get_inline_url() to handle image thumbs.
  • Loading branch information...
hypeJunction committed Nov 26, 2015
1 parent 769cb80 commit a9d409eeb7fdd9c651bde31575cc37d05db86985
@@ -76,7 +76,7 @@ public function bindSession($use_cookie = true) {
}
/**
- * Returns publically accessible URL
+ * Returns publicly accessible URL
* @return string|false
*/
public function getURL() {
View
@@ -91,6 +91,8 @@ function file_init() {
// allow to be liked
elgg_register_plugin_hook_handler('likes:is_likable', 'object:file', 'Elgg\Values::getTrue');
+
+ elgg_register_event_handler('update:after', 'object', 'file_reset_icon_urls');
}
/**
@@ -152,9 +154,23 @@ function file_page_handler($page) {
echo elgg_view_resource('file/world');
break;
case 'download':
- echo elgg_view_resource('file/download', [
- 'guid' => $page[1],
- ]);
+ elgg_deprecated_notice('/file/download page handler has been deprecated and will be removed. Use elgg_get_download_url() to build download URLs', '2.2');
+ if (_elgg_view_may_be_altered('resources/file/download', 'resources/file/download.php')) {
+ // For BC with 2.0 if a plugin is suspected of using this view we need to use it.
+ echo elgg_view_resource('file/download', [
+ 'guid' => $page[1],
+ ]);
+ } else {
+ $file = get_entity($page[1]);
+ if (!$file instanceof ElggFile) {
+ return false;
+ }
+ $download_url = elgg_get_download_url($file);
+ if (!$download_url) {
+ return false;
+ }
+ forward($download_url);
+ }
break;
default:
return false;
@@ -360,11 +376,28 @@ function file_set_icon_url($hook, $type, $url, $params) {
$file = $params['entity'];
$size = $params['size'];
if (elgg_instanceof($file, 'object', 'file')) {
-
// thumbnails get first priority
if ($file->thumbnail) {
- $ts = (int)$file->icontime;
- return "mod/file/thumbnail.php?file_guid=$file->guid&size=$size&icontime=$ts";
+ switch ($size) {
+ case "small":
+ $thumbfile = $file->thumbnail;
+ break;
+ case "medium":
+ $thumbfile = $file->smallthumb;
+ break;
+ case "large":
+ default:
+ $thumbfile = $file->largethumb;
+ break;
+ }
+
+ $readfile = new ElggFile();
+ $readfile->owner_guid = $file->owner_guid;
+ $readfile->setFilename($thumbfile);
+ $thumb_url = elgg_get_inline_url($readfile, true);
+ if ($thumb_url) {
+ return $thumb_url;
+ }
}
$mapping = array(
@@ -383,10 +416,8 @@ function file_set_icon_url($hook, $type, $url, $params) {
'application/x-rar-compressed' => 'archive',
'application/x-stuffit' => 'archive',
'application/zip' => 'archive',
-
'text/directory' => 'vcard',
'text/v-card' => 'vcard',
-
'application' => 'application',
'audio' => 'music',
'text' => 'text',
@@ -448,3 +479,38 @@ function file_handle_object_delete($event, $type, ElggObject $file) {
}
}
}
+
+/**
+ * Reset file thumb URLs if file access_id has changed
+ *
+ * @param string $event "update:after"
+ * @param string $type "object"
+ * @param ElggObject $file File entity
+ * @return void
+ */
+function file_reset_icon_urls($event, $type, ElggObject $file) {
+
+ if (!$file instanceof ElggFile) {
+ return;
+ }
+
+ $original_attributes = $file->getOriginalAttributes();
+ if (!array_key_exists('access_id', $original_attributes)) {
+ return;
+ }
+
+ // we touch the file to invalidate any previously generated download URLs
+ touch($file->getFilenameOnFilestore());
+
+ // we touch the thumbs because we want new URLs from \Elgg\FileService\File::getURL
+ $thumbnails = array($file->thumbnail, $file->smallthumb, $file->largethumb);
+ foreach ($thumbnails as $thumbnail) {
+ $thumbfile = new ElggFile();
+ $thumbfile->owner_guid = $file->owner_guid;
+ $thumbfile->setFilename($thumbnail);
+ if ($thumbfile->exists()) {
+ $thumb_filename = $thumbfile->getFilenameOnFilestore();
+ touch($thumb_filename);
+ }
+ }
+}
View
@@ -13,21 +13,21 @@
\Elgg\Application::start();
+elgg_deprecated_notice('mod/file/thumbnail.php is no longer in use and will be removed. Do not include or require it. Use elgg_get_inline_url() instead.', '2.2');
+
// Get file GUID
$file_guid = (int) get_input('file_guid', 0);
// Get file thumbnail size
$size = get_input('size', 'small');
$file = get_entity($file_guid);
-if (!elgg_instanceof($file, 'object', 'file')) {
- exit;
-}
-$simpletype = $file->simpletype;
-if ($simpletype == "image") {
+$thumb_url = false;
+
+// thumbnails get first priority
+if ($file && $file->thumbnail) {
- // Get file thumbnail
switch ($size) {
case "small":
$thumbfile = $file->thumbnail;
@@ -41,22 +41,17 @@
break;
}
- // Grab the file
- if ($thumbfile && !empty($thumbfile)) {
+ if (!empty($thumbfile)) {
$readfile = new ElggFile();
$readfile->owner_guid = $file->owner_guid;
$readfile->setFilename($thumbfile);
- $mime = $file->getMimeType();
- $contents = $readfile->grabFile();
-
- // caching images for 10 days
- header("Content-type: $mime");
- header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+10 days")), true);
- header("Pragma: public", true);
- header("Cache-Control: public", true);
- header("Content-Length: " . strlen($contents));
-
- echo $contents;
- exit;
+ $thumb_url = elgg_get_inline_url($readfile, true);
}
}
+
+if ($thumb_url) {
+ forward($thumb_url);
+}
+
+header('HTTP/1.1 404 Not found');
+exit;
@@ -14,7 +14,7 @@
return;
}
-$download_url = elgg_normalize_url("file/download/{$file->getGUID()}");
+$download_url = elgg_get_download_url($file);
$mimetype = $file->mimetype;
echo "<audio controls><source src='{$download_url}' type='{$mimetype}'></audio>";
@@ -9,7 +9,7 @@
$image_url = $file->getIconURL('large');
$image_url = elgg_format_url($image_url);
-$download_url = elgg_get_site_url() . "file/download/{$file->getGUID()}";
+$download_url = elgg_get_download_url($file);
if ($vars['full_view']) {
elgg_load_js('lightbox');
@@ -3,8 +3,11 @@
* Elgg file download.
*
* @package ElggFile
+ * @deprecated since version 3.0
*/
+elgg_deprecated_notice('/file/download resource view has been deprecated and will be removed. Use elgg_get_download_url() to build download URLs', '2.2');
+
// Get the guid
$file_guid = elgg_extract("guid", $vars);
@@ -15,23 +18,4 @@
forward();
}
-$mime = $file->getMimeType();
-if (!$mime) {
- $mime = "application/octet-stream";
-}
-
-$filename = $file->originalfilename;
-
-// fix for IE https issue
-header("Pragma: public");
-
-header("Content-type: $mime");
-header("Content-Disposition: attachment; filename=\"$filename\"");
-header("Content-Length: {$file->getSize()}");
-
-while (ob_get_level()) {
- ob_end_clean();
-}
-flush();
-readfile($file->getFilenameOnFilestore());
-exit;
+forward(elgg_get_download_url($file));
@@ -34,7 +34,7 @@
elgg_register_menu_item('title', array(
'name' => 'download',
'text' => elgg_echo('download'),
- 'href' => "file/download/$file->guid",
+ 'href' => elgg_get_download_url($file),
'link_class' => 'elgg-button elgg-button-action',
));
@@ -6,7 +6,7 @@
*/
if (elgg_instanceof($vars['entity'], 'object', 'file')) {
- $download_url = elgg_get_site_url() . 'file/download/' . $vars['entity']->getGUID();
+ $download_url = elgg_get_download_url($vars['entity']);
$size = $vars['entity']->getSize();
$mime_type = $vars['entity']->getMimeType();
echo <<<END

0 comments on commit a9d409e

Please sign in to comment.