3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Api\Friendica\Photo;
25 use Friendica\Core\ACL;
26 use Friendica\Core\L10n;
27 use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
28 use Friendica\Module\BaseApi;
29 use Friendica\Model\Photo;
30 use Friendica\Module\Api\ApiResponse;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
36 * API endpoint: /api/friendica/photo/create
38 class Create extends BaseApi
40 /** @var FriendicaPhoto */
41 private $friendicaPhoto;
44 public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
46 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48 $this->friendicaPhoto = $friendicaPhoto;
51 protected function post(array $request = [])
53 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
54 $uid = BaseApi::getCurrentUserID();
55 $type = $this->getRequestValue($this->parameters, 'extension', 'json');
58 $desc = $this->getRequestValue($request, 'desc') ?? '';
59 $album = $this->getRequestValue($request, 'album');
60 $allow_cid = $this->getRequestValue($request, 'allow_cid');
61 $deny_cid = $this->getRequestValue($request, 'deny_cid');
62 $allow_gid = $this->getRequestValue($request, 'allow_gid');
63 $deny_gid = $this->getRequestValue($request, 'deny_gid');
65 // do several checks on input parameters
66 // we do not allow calls without album string
68 throw new HTTPException\BadRequestException('no albumname specified');
71 // error if no media posted in create-mode
72 if (empty($_FILES['media'])) {
74 throw new HTTPException\BadRequestException('no media data submitted');
77 // checks on acl strings provided by clients
78 $acl_input_error = false;
79 $acl_input_error |= !ACL::isValidContact($allow_cid, $uid);
80 $acl_input_error |= !ACL::isValidContact($deny_cid, $uid);
81 $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid);
82 $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid);
83 if ($acl_input_error) {
84 throw new HTTPException\BadRequestException('acl data invalid');
86 // now let's upload the new media in create-mode
87 $photo = Photo::upload($uid, $_FILES['media'], $album, trim($allow_cid), trim($allow_gid), trim($deny_cid), trim($deny_gid), $desc);
89 // return success of updating or error message
91 $data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)];
92 $this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null);
94 throw new HTTPException\InternalServerErrorException('unknown error - uploading photo failed, see Friendica log for more information');