123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class AttachmentAction extends ManagedAction
- {
-
- var $attachment = null;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- try {
- if (!empty($id = $this->trimmed('attachment'))) {
- $this->attachment = File::getByID($id);
- } elseif (!empty($filehash = $this->trimmed('filehash'))) {
- $this->attachment = File::getByHash($filehash);
- }
- } catch (Exception $e) {
-
- }
- if (!$this->attachment instanceof File) {
-
- $this->clientError(_('No such attachment.'), 404);
- }
- $filename = $this->attachment->getFileOrThumbnailPath();
- if (empty($filename)) {
- $this->clientError(_('Requested local URL for a file that is not stored locally.'), 404);
- }
- return true;
- }
-
- function isReadOnly($args)
- {
- return true;
- }
-
- function title()
- {
- $a = new Attachment($this->attachment);
- return $a->title();
- }
- public function showPage()
- {
- if (empty($this->attachment->getFileOrThumbnailPath())) {
-
- common_redirect($this->attachment->getUrl(), 303);
- }
- parent::showPage();
- }
-
- function showContent()
- {
- $ali = new Attachment($this->attachment, $this);
- $cnt = $ali->show();
- }
-
- function showPageNoticeBlock()
- {
- }
-
- function showSections() {
- $ns = new AttachmentNoticeSection($this);
- $ns->show();
- }
-
- public function lastModified()
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- $path = $this->attachment->getFileOrThumbnailPath();
- if (!empty($path)) {
- return filemtime($path);
- } else {
- return null;
- }
- }
-
- function etag()
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- $path = $this->attachment->getFileOrThumbnailPath();
- $cache = Cache::instance();
- if($cache) {
- if (empty($path)) {
- return null;
- }
- $key = Cache::key('attachments:etag:' . $path);
- $etag = $cache->get($key);
- if($etag === false) {
- $etag = crc32(file_get_contents($path));
- $cache->set($key,$etag);
- }
- return $etag;
- }
- if (!empty($path)) {
- $stat = stat($path);
- return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
- } else {
- return null;
- }
- }
-
- static function sendFile(string $filepath, $filesize) {
- if (is_string(common_config('site', 'x-static-delivery'))) {
- $tmp = explode(INSTALLDIR, $filepath);
- $relative_path = end($tmp);
- common_debug("Using Static Delivery with header: '" .
- common_config('site', 'x-static-delivery') . ": {$relative_path}'");
- header(common_config('site', 'x-static-delivery') . ": {$relative_path}");
- } else {
- if (empty($filesize)) {
- $filesize = filesize($filepath);
- }
- header("Content-Length: {$filesize}");
-
- $ret = @readfile($filepath);
- if ($ret === false) {
- common_log(LOG_ERR, "Couldn't read file at {$filepath}.");
- } elseif ($ret !== $filesize) {
- common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
- "{$filepath} differ from what was sent to the user ({$filesize} vs {$ret}).");
- }
- }
- }
- }
|