]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Photo/Create.php
c8ea2fbd78406e290fee2d8c5d862b4f06b84176
[friendica.git] / src / Module / Api / Friendica / Photo / Create.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Photo;
23
24 use Friendica\App;
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;
34
35 /**
36  * API endpoint: /api/friendica/photo/create
37  */
38 class Create extends BaseApi
39 {
40         /** @var FriendicaPhoto */
41         private $friendicaPhoto;
42
43
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 = [])
45         {
46                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
47
48                 $this->friendicaPhoto = $friendicaPhoto;
49         }
50
51         protected function post(array $request = [])
52         {
53                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
54                 $uid  = BaseApi::getCurrentUserID();
55                 $type = $this->getRequestValue($this->parameters, 'extension', 'json');
56
57                 // input params 
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');
64
65                 // do several checks on input parameters
66                 // we do not allow calls without album string
67                 if ($album == null) {
68                         throw new HTTPException\BadRequestException('no albumname specified');
69                 }
70
71                 // error if no media posted in create-mode
72                 if (empty($_FILES['media'])) {
73                         // Output error
74                         throw new HTTPException\BadRequestException('no media data submitted');
75                 }
76
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');
85                 }
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);
88
89                 // return success of updating or error message
90                 if (!empty($photo)) {
91                         $data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)];
92                         $this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null);
93                         return;
94                 } else {
95                         throw new HTTPException\InternalServerErrorException('unknown error - uploading photo failed, see Friendica log for more information');
96                 }
97         }
98 }