3 if (!defined('GNUSOCIAL')) { exit(1); }
6 * Download notice attachment
10 * @author Mikael Nordfeldth <mmn@hethane.se>
11 * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
12 * @link https:/gnu.io/social
14 class Attachment_viewAction extends AttachmentAction
16 public function showPage()
18 // Checks file exists or throws FileNotStoredLocallyException
19 $filepath = $this->attachment->getPath();
21 $filename = MediaFile::getDisplayName($this->attachment);
23 // Disable errors, to not mess with the file contents (suppress errors in case access to this
24 // function is blocked, like in some shared hosts). Automatically reset at the end of the
25 // script execution, and we don't want to have any more errors until then, so don't reset it
26 @ini_set('display_errors', 0);
28 header("Content-Description: File Transfer");
29 header("Content-Type: {$this->attachment->mimetype}");
30 if (in_array(common_get_mime_media($this->attachment->mimetype), ['image', 'video'])) {
31 header("Content-Disposition: inline; filename=\"{$filename}\"");
33 header("Content-Disposition: attachment; filename=\"{$filename}\"");
36 header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
37 $filesize = $this->attachment->size;
38 // 'if available', it says, so ensure we have it
39 if (empty($filesize)) {
40 $filesize = filesize($this->attachment->filename);
42 header("Content-Length: {$filesize}");
43 // header('Cache-Control: private, no-transform, no-store, must-revalidate');
45 $ret = @readfile($filepath);
47 if ($ret === false || $ret !== $filesize) {
48 common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
49 "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user.");