Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
... ... @@ -1,6 +1,12 @@
  1 +#default variants
1 2 attachmentserver_variants:
2 3 thumbnail:
3 4 scale:
4 5 width: 150
5 6 height: 150
  7 +# in case of an empty attachment we will show this image as a placeholder
6 8 attachmentserver_placeholder_url: __MIDGARDMVC_STATIC_URL__/midgardmvc_helper_attachmentserver/placeholder.png
  9 +# each attachment will be served with the HTTP header "Cache-Control: maxage" set to this value
  10 +# also the Expires HTTP header will be set by adding maxage to the current date
  11 +# value is in seconds (86400 equals to 1 day)
  12 +attachment_maxage: 86400
112 controllers/base.php
@@ -18,67 +18,77 @@ public function __construct(midgardmvc_core_request $request)
18 18 $this->request = $request;
19 19 }
20 20
21   - public function serve_attachment(midgard_attachment $att)
  21 + public function serve_attachment(midgard_attachment $att = null)
22 22 {
23   - if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache)
  23 + if ($att)
24 24 {
25   - // Relocate to attachment serving URL
26   - midgardmvc_core::get_instance()->head->relocate(midgardmvc_core_helpers_attachment::get_url($att));
27   - }
28 25
29   - $blob = midgardmvc_helper_attachmentserver_helpers::get_blob($att);
30   - $stream = $blob->get_handler('rb');
31   - $mtime = $att->metadata->revised->format('r');
  26 + if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache)
  27 + {
  28 + // Relocate to attachment serving URL
  29 + midgardmvc_core::get_instance()->head->relocate(midgardmvc_core_helpers_attachment::get_url($att));
  30 + }
32 31
33   - // Generate ETag
34   - $hash = hash_init('sha1');
35   - hash_update_stream($hash, $stream);
36   - hash_update($hash, $mtime);
37   - $etag = hash_final($hash);
38   - fclose($stream);
  32 + $blob = midgardmvc_helper_attachmentserver_helpers::get_blob($att);
  33 + $stream = $blob->get_handler('rb');
  34 + $mtime = $att->metadata->revised->format('r');
39 35
40   - if ( isset($_SERVER['HTTP_IF_NONE_MATCH'])
41   - && $etag == $_SERVER['HTTP_IF_NONE_MATCH'])
42   - {
43   - throw new midgardmvc_exception_httperror("File has not changed", 304);
44   - }
  36 + // Generate ETag
  37 + $hash = hash_init('sha1');
  38 + hash_update_stream($hash, $stream);
  39 + hash_update($hash, $mtime);
  40 + $etag = hash_final($hash);
  41 + fclose($stream);
45 42
46   - if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
47   - && $mtime == $_SERVER['HTTP_IF_MODIFIED_SINCE'])
48   - {
49   - throw new midgardmvc_exception_httperror("Not modified since {$mtime}", 304);
50   - }
  43 + if ( isset($_SERVER['HTTP_IF_NONE_MATCH'])
  44 + && $etag == $_SERVER['HTTP_IF_NONE_MATCH'])
  45 + {
  46 + throw new midgardmvc_exception_httperror("File has not changed", 304);
  47 + }
51 48
52   - // Safety for broken mimetypes
53   - if (empty($att->mimetype))
54   - {
55   - $att->mimetype = midgardmvc_helper_attachmentserver_helpers::resolve_mime_type($blob->get_path());
56   - midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_helper_attachmentserver');
57   - $att->update();
58   - midgardmvc_core::get_instance()->authorization->leave_sudo();
59   - }
  49 + if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
  50 + && $mtime == $_SERVER['HTTP_IF_MODIFIED_SINCE'])
  51 + {
  52 + throw new midgardmvc_exception_httperror("Not modified since {$mtime}", 304);
  53 + }
60 54
61   - midgardmvc_core::get_instance()->dispatcher->header('Content-type: '.$att->mimetype);
62   - midgardmvc_core::get_instance()->dispatcher->header('ETag: '.$etag);
63   - midgardmvc_core::get_instance()->dispatcher->header('Last-Modified: '.$mtime);
  55 + // Safety for broken mimetypes
  56 + if (empty($att->mimetype))
  57 + {
  58 + $att->mimetype = midgardmvc_helper_attachmentserver_helpers::resolve_mime_type($blob->get_path());
  59 + midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_helper_attachmentserver');
  60 + $att->update();
  61 + midgardmvc_core::get_instance()->authorization->leave_sudo();
  62 + }
64 63
65   - /**
66   - * If X-Sendfile support is enabled just send correct headers
67   - */
68   - if (midgardmvc_core::get_instance()->configuration->enable_xsendfile)
69   - {
70   - midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path());
71   - }
72   - else
73   - {
74   - // TODO: Investigate ways to pass the file info in smaller chunks and outside of the output buffer (think 100MB files...)
75   - echo $blob->read_content();
76   - /*
77   - //$stream = $blob->get_handler('rb');
78   - $stream->rewind();
79   - fpassthru($stream);
80   - */
  64 + midgardmvc_core::get_instance()->dispatcher->header('Content-type: '.$att->mimetype);
  65 + midgardmvc_core::get_instance()->dispatcher->header('ETag: '.$etag);
  66 + midgardmvc_core::get_instance()->dispatcher->header('Last-Modified: '.$mtime);
  67 +
  68 + $maxage = midgardmvc_core::get_instance()->configuration->attachment_maxage;
  69 + midgardmvc_core::get_instance()->dispatcher->header("Pragma: public");
  70 + midgardmvc_core::get_instance()->dispatcher->header("Cache-Control: maxage=" . $maxage);
  71 + midgardmvc_core::get_instance()->dispatcher->header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $maxage) . ' +0000');
  72 +
  73 + /**
  74 + * If X-Sendfile support is enabled just send correct headers
  75 + */
  76 + if (midgardmvc_core::get_instance()->configuration->enable_xsendfile)
  77 + {
  78 + midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path());
  79 + }
  80 + else
  81 + {
  82 + // TODO: Investigate ways to pass the file info in smaller chunks and outside of the output buffer (think 100MB files...)
  83 + echo $blob->read_content();
  84 + /*
  85 + //$stream = $blob->get_handler('rb');
  86 + $stream->rewind();
  87 + fpassthru($stream);
  88 + */
  89 + }
81 90 }
  91 +
82 92 midgardmvc_core::get_instance()->dispatcher->end_request();
83 93 }
84 94 }
9 controllers/simple.php
@@ -20,7 +20,14 @@ class midgardmvc_helper_attachmentserver_controllers_simple extends midgardmvc_h
20 20 */
21 21 public function get_file(array $args)
22 22 {
23   - $att = new midgard_attachment($args['guid']);
  23 + try
  24 + {
  25 + $att = new midgard_attachment($args['guid']);
  26 + }
  27 + catch (midgard_error_exception $e)
  28 + {
  29 + throw new midgardmvc_exception_notfound("Attachment not found: " . $e->getMessage());
  30 + }
24 31
25 32 $this->serve_attachment($att);
26 33 }
11 helpers.php
@@ -104,6 +104,15 @@ public static function variant_is_fresh(midgard_attachment $variant, $parent=nul
104 104 */
105 105 public static function generate_variant(midgard_attachment $original, $variant, $force_regenerate = false)
106 106 {
  107 + $original_blob = midgardmvc_helper_attachmentserver_helpers::get_blob($original);
  108 +
  109 + if (! filesize($original_blob->get_path()))
  110 + {
  111 + // we can't do much with 0 byte long files
  112 + midgardmvc_core::get_instance()->log('The blob: ' . $original_blob->get_path() . ' is 0 byte long, can not process it.', 'error');
  113 + return null;
  114 + }
  115 +
107 116 $old_variant = midgardmvc_helper_attachmentserver_helpers::get_variant($original, $variant);
108 117 if ($old_variant !== false)
109 118 {
@@ -135,7 +144,6 @@ public static function generate_variant(midgard_attachment $original, $variant,
135 144 throw new midgardmvc_exception("Variant {$variant} is not defined");
136 145 }
137 146
138   - $original_blob = midgardmvc_helper_attachmentserver_helpers::get_blob($original);
139 147 if (empty($original->mimetype))
140 148 {
141 149 $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,
152 160 $converter->createTransformation($variant, $filters, array($original->mimetype));
153 161
154 162 $transformed_image = tempnam(sys_get_temp_dir(), "{$original->guid}_{$variant}");
  163 +
155 164 $converter->transform
156 165 (
157 166 $variant,

0 comments on commit 768d54e

Please sign in to comment.
Something went wrong with that request. Please try again.