3 * @file src/Module/Attach.php
7 namespace Friendica\Module;
9 use Friendica\BaseModule;
10 use Friendica\Core\Logger;
12 use Friendica\Model\Attach as MAttach;
17 class Attach extends BaseModule
20 * Return to user an attached file given the id
22 public static function rawContent(array $parameters = [])
26 throw new \Friendica\Network\HTTPException\BadRequestException();
29 // @TODO: Replace with parameter from router
30 $item_id = intval($a->argv[1]);
32 // Check for existence
33 $item = MAttach::exists(['id' => $item_id]);
34 if ($item === false) {
35 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
38 // Now we'll fetch the item, if we have enough permisson
39 $item = MAttach::getByIdWithPermission($item_id);
40 if ($item === false) {
41 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
44 $data = MAttach::getData($item);
46 Logger::log('NULL data for attachment with id ' . $item['id']);
47 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
50 // Use quotes around the filename to prevent a "multiple Content-Disposition"
51 // error in Chrome for filenames with commas in them
52 header('Content-type: ' . $item['filetype']);
53 header('Content-length: ' . $item['filesize']);
54 if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
55 header('Content-disposition: filename="' . $item['filename'] . '"');
57 header('Content-disposition: attachment; filename="' . $item['filename'] . '"');