123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- defined('GNUSOCIAL') || die();
- class AttachmentAction extends ManagedAction
- {
-
- public $attachment = null;
- public $filehash = null;
- public $filepath = null;
- public $filesize = null;
- public $mimetype = null;
- public $filename = null;
-
- protected function prepare(array $args = [])
- {
- parent::prepare($args);
- try {
- if (!empty($id = $this->trimmed('attachment'))) {
- $this->attachment = File::getByID($id);
- } elseif (!empty($this->filehash = $this->trimmed('filehash'))) {
- $this->attachment = File::getByHash($this->filehash);
- }
- } catch (Exception $e) {
-
- }
- if (!$this->attachment instanceof File) {
-
- $this->clientError(_m('No such attachment.'), 404);
- }
- $this->filepath = $this->attachment->getFileOrThumbnailPath();
- if (empty($this->filepath)) {
- $this->clientError(_m('Requested local URL for a file that is not stored locally.'), 404);
- }
- $this->filesize = $this->attachment->getFileOrThumbnailSize();
- $this->mimetype = $this->attachment->getFileOrThumbnailMimetype();
- $this->filename = MediaFile::getDisplayName($this->attachment);
- return true;
- }
-
- public function isReadOnly($args): bool
- {
- return true;
- }
-
- public function title(): string
- {
- $a = new Attachment($this->attachment);
- return $a->title();
- }
- public function showPage(): void
- {
- if (empty($this->filepath)) {
-
- common_redirect($this->attachment->getUrl(), 303);
- }
- parent::showPage();
- }
-
- public function showContent(): void
- {
- $ali = new Attachment($this->attachment, $this);
- $ali->show();
- }
-
- public function showPageNoticeBlock(): void
- {
- }
-
- public function showSections(): void
- {
- $ns = new AttachmentNoticeSection($this);
- $ns->show();
- }
-
- public function lastModified(): ?int
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- $path = $this->filepath;
- if (!empty($path)) {
- return filemtime($path);
- } else {
- return null;
- }
- }
-
- public function etag(): ?string
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- $path = $this->filepath;
- $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;
- }
- }
- }
|