diff --git a/src/flextype/app/Endpoints/files.php b/src/flextype/app/Endpoints/files.php index 34b8f626..56b36555 100644 --- a/src/flextype/app/Endpoints/files.php +++ b/src/flextype/app/Endpoints/files.php @@ -282,6 +282,95 @@ $app->put('/api/files', function (Request $request, Response $response) use ($fl ->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); }); +/** + * Copy media file + * + * endpoint: PUT /api/files + * + * Body: + * id - [REQUIRED] - Unique identifier of the file. + * new_id - [REQUIRED] - New Unique identifier of the file. + * token - [REQUIRED] - Valid Entries token. + * access_token - [REQUIRED] - Valid Access token. + * + * Returns: + * Returns the file object for the file that was just created. + */ +$app->put('/api/files/copy', function (Request $request, Response $response) use ($flextype, $api_errors) { + // Get Post Data + $post_data = $request->getParsedBody(); + + if (! isset($post_data['token']) || ! isset($post_data['access_token']) || ! isset($post_data['path']) || ! isset($post_data['new_path'])) { + return $response->withJson($api_errors['0501'], $api_errors['0501']['http_status_code']); + } + + // Set variables + $token = $post_data['token']; + $access_token = $post_data['access_token']; + $path = $post_data['path']; + $new_path = $post_data['new_path']; + + if ($flextype['registry']->get('flextype.settings.api.files.enabled')) { + // Validate files and access token + if (validate_files_token($token) && validate_access_token($access_token)) { + $files_token_file_path = PATH['project'] . '/tokens/files/' . $token . '/token.yaml'; + $access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml'; + + // Set files and access token file + if (($files_token_file_data = $flextype['yaml']->decode(Filesystem::read($files_token_file_path))) && + ($access_token_file_data = $flextype['yaml']->decode(Filesystem::read($access_token_file_path)))) { + if ($files_token_file_data['state'] === 'disabled' || + ($files_token_file_data['limit_calls'] !== 0 && $files_token_file_data['calls'] >= $files_token_file_data['limit_calls'])) { + return $response->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); + } + + if ($access_token_file_data['state'] === 'disabled' || + ($access_token_file_data['limit_calls'] !== 0 && $access_token_file_data['calls'] >= $access_token_file_data['limit_calls'])) { + return $response->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); + } + + // Copy file + $copy_file = $flextype['media_files']->copy($path, $new_path); + + if ($copy_file) { + $response_data['data'] = $flextype['media_files']->fetch($new_path); + } else { + $response_data['data'] = []; + } + + // Set response code + $response_code = ($copy_file === true) ? 200 : 404; + + // Return response + return $response + ->withJson($response_data, $response_code); + + // Update calls counter + Filesystem::write($files_token_file_path, $flextype['yaml']->encode(array_replace_recursive($files_token_file_data, ['calls' => $files_token_file_data['calls'] + 1]))); + + if ($response_code === 404) { + // Return response + return $response + ->withJson($api_errors['0502'], $api_errors['0502']['http_status_code']); + } + + // Return response + return $response + ->withJson($response_data, $response_code); + } + + return $response + ->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); + } + + return $response + ->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); + } + + return $response + ->withJson($api_errors['0003'], $api_errors['0003']['http_status_code']); +}); + /** * Delete file * diff --git a/src/flextype/app/Foundation/Media/MediaFiles.php b/src/flextype/app/Foundation/Media/MediaFiles.php index e98c8e26..799472f6 100644 --- a/src/flextype/app/Foundation/Media/MediaFiles.php +++ b/src/flextype/app/Foundation/Media/MediaFiles.php @@ -372,6 +372,30 @@ class MediaFiles Filesystem::has($this->flextype['media_files_meta']->getFileMetaLocation($id)); } + /** + * Copy file + * + * @param string $id Unique identifier of the file. + * @param string $new_id New Unique identifier of the file. + * + * @return bool True on success, false on failure. + * + * @access public + */ + public function copy(string $id, string $new_id) : bool + { + if (! Filesystem::has($this->getFileLocation($new_id)) && ! Filesystem::has($this->flextype['media_files_meta']->getFileMetaLocation($new_id))) { + Filesystem::copy($this->getFileLocation($id), + $this->getFileLocation($new_id), false); + Filesystem::copy($this->flextype['media_files_meta']->getFileMetaLocation($id), + $this->flextype['media_files_meta']->getFileMetaLocation($new_id), false); + + return (Filesystem::has($this->getFileLocation($new_id)) && Filesystem::has($this->flextype['media_files_meta']->getFileMetaLocation($new_id)) === true) ? true : false; + } + + return false; + } + /** * Get file location *