Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10 from feri/master

Safety and cache control
  • Loading branch information...
commit 768d54e9cddd753e7d026e7b8ebb2a99d82b81e8 2 parents a4b7136 + 92902b7
Henri Bergius bergie authored
6 configuration/defaults.yml
View
@@ -1,6 +1,12 @@
+#default variants
attachmentserver_variants:
thumbnail:
scale:
width: 150
height: 150
+# in case of an empty attachment we will show this image as a placeholder
attachmentserver_placeholder_url: __MIDGARDMVC_STATIC_URL__/midgardmvc_helper_attachmentserver/placeholder.png
+# each attachment will be served with the HTTP header "Cache-Control: maxage" set to this value
+# also the Expires HTTP header will be set by adding maxage to the current date
+# value is in seconds (86400 equals to 1 day)
+attachment_maxage: 86400
112 controllers/base.php
View
@@ -18,67 +18,77 @@ public function __construct(midgardmvc_core_request $request)
$this->request = $request;
}
- public function serve_attachment(midgard_attachment $att)
+ public function serve_attachment(midgard_attachment $att = null)
{
- if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache)
+ if ($att)
{
- // Relocate to attachment serving URL
- midgardmvc_core::get_instance()->head->relocate(midgardmvc_core_helpers_attachment::get_url($att));
- }
- $blob = midgardmvc_helper_attachmentserver_helpers::get_blob($att);
- $stream = $blob->get_handler('rb');
- $mtime = $att->metadata->revised->format('r');
+ if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache)
+ {
+ // Relocate to attachment serving URL
+ midgardmvc_core::get_instance()->head->relocate(midgardmvc_core_helpers_attachment::get_url($att));
+ }
- // Generate ETag
- $hash = hash_init('sha1');
- hash_update_stream($hash, $stream);
- hash_update($hash, $mtime);
- $etag = hash_final($hash);
- fclose($stream);
+ $blob = midgardmvc_helper_attachmentserver_helpers::get_blob($att);
+ $stream = $blob->get_handler('rb');
+ $mtime = $att->metadata->revised->format('r');
- if ( isset($_SERVER['HTTP_IF_NONE_MATCH'])
- && $etag == $_SERVER['HTTP_IF_NONE_MATCH'])
- {
- throw new midgardmvc_exception_httperror("File has not changed", 304);
- }
+ // Generate ETag
+ $hash = hash_init('sha1');
+ hash_update_stream($hash, $stream);
+ hash_update($hash, $mtime);
+ $etag = hash_final($hash);
+ fclose($stream);
- if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
- && $mtime == $_SERVER['HTTP_IF_MODIFIED_SINCE'])
- {
- throw new midgardmvc_exception_httperror("Not modified since {$mtime}", 304);
- }
+ if ( isset($_SERVER['HTTP_IF_NONE_MATCH'])
+ && $etag == $_SERVER['HTTP_IF_NONE_MATCH'])
+ {
+ throw new midgardmvc_exception_httperror("File has not changed", 304);
+ }
- // Safety for broken mimetypes
- if (empty($att->mimetype))
- {
- $att->mimetype = midgardmvc_helper_attachmentserver_helpers::resolve_mime_type($blob->get_path());
- midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_helper_attachmentserver');
- $att->update();
- midgardmvc_core::get_instance()->authorization->leave_sudo();
- }
+ if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
+ && $mtime == $_SERVER['HTTP_IF_MODIFIED_SINCE'])
+ {
+ throw new midgardmvc_exception_httperror("Not modified since {$mtime}", 304);
+ }
- midgardmvc_core::get_instance()->dispatcher->header('Content-type: '.$att->mimetype);
- midgardmvc_core::get_instance()->dispatcher->header('ETag: '.$etag);
- midgardmvc_core::get_instance()->dispatcher->header('Last-Modified: '.$mtime);
+ // Safety for broken mimetypes
+ if (empty($att->mimetype))
+ {
+ $att->mimetype = midgardmvc_helper_attachmentserver_helpers::resolve_mime_type($blob->get_path());
+ midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_helper_attachmentserver');
+ $att->update();
+ midgardmvc_core::get_instance()->authorization->leave_sudo();
+ }
- /**
- * If X-Sendfile support is enabled just send correct headers
- */
- if (midgardmvc_core::get_instance()->configuration->enable_xsendfile)
- {
- midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path());
- }
- else
- {
- // TODO: Investigate ways to pass the file info in smaller chunks and outside of the output buffer (think 100MB files...)
- echo $blob->read_content();
- /*
- //$stream = $blob->get_handler('rb');
- $stream->rewind();
- fpassthru($stream);
- */
+ midgardmvc_core::get_instance()->dispatcher->header('Content-type: '.$att->mimetype);
+ midgardmvc_core::get_instance()->dispatcher->header('ETag: '.$etag);
+ midgardmvc_core::get_instance()->dispatcher->header('Last-Modified: '.$mtime);
+
+ $maxage = midgardmvc_core::get_instance()->configuration->attachment_maxage;
+ midgardmvc_core::get_instance()->dispatcher->header("Pragma: public");
+ midgardmvc_core::get_instance()->dispatcher->header("Cache-Control: maxage=" . $maxage);
+ midgardmvc_core::get_instance()->dispatcher->header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $maxage) . ' +0000');
+
+ /**
+ * If X-Sendfile support is enabled just send correct headers
+ */
+ if (midgardmvc_core::get_instance()->configuration->enable_xsendfile)
+ {
+ midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path());
+ }
+ else
+ {
+ // TODO: Investigate ways to pass the file info in smaller chunks and outside of the output buffer (think 100MB files...)
+ echo $blob->read_content();
+ /*
+ //$stream = $blob->get_handler('rb');
+ $stream->rewind();
+ fpassthru($stream);
+ */
+ }
}
+
midgardmvc_core::get_instance()->dispatcher->end_request();
}
}
9 controllers/simple.php
View
@@ -20,7 +20,14 @@ class midgardmvc_helper_attachmentserver_controllers_simple extends midgardmvc_h
*/
public function get_file(array $args)
{
- $att = new midgard_attachment($args['guid']);
+ try
+ {
+ $att = new midgard_attachment($args['guid']);
+ }
+ catch (midgard_error_exception $e)
+ {
+ throw new midgardmvc_exception_notfound("Attachment not found: " . $e->getMessage());
+ }
$this->serve_attachment($att);
}
11 helpers.php
View
@@ -104,6 +104,15 @@ public static function variant_is_fresh(midgard_attachment $variant, $parent=nul
*/
public static function generate_variant(midgard_attachment $original, $variant, $force_regenerate = false)
{
+ $original_blob = midgardmvc_helper_attachmentserver_helpers::get_blob($original);
+
+ if (! filesize($original_blob->get_path()))
+ {
+ // we can't do much with 0 byte long files
+ midgardmvc_core::get_instance()->log('The blob: ' . $original_blob->get_path() . ' is 0 byte long, can not process it.', 'error');
+ return null;
+ }
+
$old_variant = midgardmvc_helper_attachmentserver_helpers::get_variant($original, $variant);
if ($old_variant !== false)
{
@@ -135,7 +144,6 @@ public static function generate_variant(midgard_attachment $original, $variant,
throw new midgardmvc_exception("Variant {$variant} is not defined");
}
- $original_blob = midgardmvc_helper_attachmentserver_helpers::get_blob($original);
if (empty($original->mimetype))
{
$original->mimetype = midgardmvc_helper_attachmentserver_helpers::resolve_mime_type($original_blob->get_path());
@@ -152,6 +160,7 @@ public static function generate_variant(midgard_attachment $original, $variant,
$converter->createTransformation($variant, $filters, array($original->mimetype));
$transformed_image = tempnam(sys_get_temp_dir(), "{$original->guid}_{$variant}");
+
$converter->transform
(
$variant,
Please sign in to comment.
Something went wrong with that request. Please try again.