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;
24 use Friendica\Core\ACL;
26 use Friendica\Model\Photo;
27 use Friendica\Module\BaseApi;
28 use Friendica\Network\HTTPException;
31 * API endpoint: /api/friendica/photo/create
33 class Create extends BaseApi
35 protected function post(array $request = [])
37 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
38 $uid = BaseApi::getCurrentUserID();
39 $type = $this->parameters['extension'] ?? '';
42 $desc = $_REQUEST['desc'] ?? null;
43 $album = $_REQUEST['album'] ?? null;
44 $allow_cid = $_REQUEST['allow_cid'] ?? null;
45 $deny_cid = $_REQUEST['deny_cid' ] ?? null;
46 $allow_gid = $_REQUEST['allow_gid'] ?? null;
47 $deny_gid = $_REQUEST['deny_gid' ] ?? null;
49 // do several checks on input parameters
50 // we do not allow calls without album string
52 throw new HTTPException\BadRequestException('no albumname specified');
55 // error if no media posted in create-mode
56 if (empty($_FILES['media'])) {
58 throw new HTTPException\BadRequestException('no media data submitted');
61 // checks on acl strings provided by clients
62 $acl_input_error = false;
63 $acl_input_error |= !ACL::isValidContact($allow_cid, $uid);
64 $acl_input_error |= !ACL::isValidContact($deny_cid, $uid);
65 $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid);
66 $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid);
67 if ($acl_input_error) {
68 throw new HTTPException\BadRequestException('acl data invalid');
70 // now let's upload the new media in create-mode
71 $photo = Photo::upload($uid, $_FILES['media'], $album, trim($allow_cid), trim($allow_gid), trim($deny_cid), trim($deny_gid), $desc);
73 // return success of updating or error message
75 $data = ['photo' => DI::friendicaPhoto()->createFromId($photo['resource_id'], null, $uid, $type)];
76 $this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null);
79 throw new HTTPException\InternalServerErrorException('unknown error - uploading photo failed, see Friendica log for more information');