Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate attachment upload by file path #302

Merged
merged 8 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Switched from Travis-CI to Github Actions

### Fixed

- Avoid warning if path of uploaded file is longer than the maximum allowed path length

### Deprecated

- `Redmine\Api\AbstractApi::lastCallFailed()` is deprecated, use `Redmine\Client\Client::getLastResponseStatusCode()` instead
- Uploading an attachment using `Redmine\Api\Attachment::upload()` with filepath is deprectead, use `file_get_contents()` to upload the file content instead

## [v2.0.1](https://github.com/kbsali/php-redmine-api/compare/v2.0.0...v2.0.1) - 2021-09-22

Expand Down
17 changes: 17 additions & 0 deletions src/Redmine/Client/ClientApiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ public function getApi(string $name): Api

return $this->apiInstances[$name];
}

private function isUploadCall(string $path): bool
{
$path = strtolower($path);

return (false !== strpos($path, '/uploads.json')) or (false !== strpos($path, '/uploads.xml'));
Art4 marked this conversation as resolved.
Show resolved Hide resolved
}

private function isUploadCallAndFilepath(string $path, string $body): bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the method is not very clear to me. What do you mean with "AndFilePath"?

Copy link
Collaborator Author

@Art4 Art4 Jan 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method checks if $path is an upload call and if $body contains a valid file path using is_file(), but was previously named isUploadCall() which does not fully describe the function. I extracted the upload check into its own method (named isUploadCall()) and now I needed a name that better described the meaning of the previous method. That is the reason why I came up with isUploadCallAndFilepath().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should remove the isUploadCall() from this method and rename isUploadCallAndFilepath() to isFilepath()?

Copy link
Owner

@kbsali kbsali Jan 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isValidFilePath ?
isAuthorizedFilePath ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove the call of isUploadCall() and rename it to isValidFilePath().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{
return
$this->isUploadCall($path)
&& '' !== $body
&& strlen($body) <= \PHP_MAXPATHLEN
&& is_file(strval(str_replace("\0", '', $body)))
;
}
}
15 changes: 4 additions & 11 deletions src/Redmine/Client/NativeCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ private function createCurl(string $method, string $path, string $body = '')
switch ($method) {
case 'post':
$curlOptions[CURLOPT_POST] = 1;
if ($this->isUploadCall($path, $body)) {
if ($this->isUploadCallAndFilepath($path, $body)) {
@trigger_error('Upload an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED);
Art4 marked this conversation as resolved.
Show resolved Hide resolved

$file = fopen($body, 'r');
$size = filesize($body);
$filedata = fread($file, $size);
Expand Down Expand Up @@ -311,15 +313,6 @@ private function createCurl(string $method, string $path, string $body = '')
return $curl;
}

private function isUploadCall(string $path, string $body): bool
{
return
(preg_match('/\/uploads.(json|xml)/i', $path)) &&
'' !== $body &&
is_file(strval(str_replace("\0", '', $body)))
;
}

private function createHttpHeader(string $path): array
{
// Additional request headers
Expand Down Expand Up @@ -360,7 +353,7 @@ private function createHttpHeader(string $path): array
// Content type headers
$tmp = parse_url($this->url.$path);

if (preg_match('/\/uploads.(json|xml)/i', $path)) {
if ($this->isUploadCall($path)) {
$httpHeaders[] = 'Content-Type: application/octet-stream';
} elseif ('json' === substr($tmp['path'], -4)) {
$httpHeaders[] = 'Content-Type: application/json';
Expand Down
15 changes: 4 additions & 11 deletions src/Redmine/Client/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ private function createRequest(string $method, string $path, string $body = ''):

switch ($method) {
case 'POST':
if ($this->isUploadCall($path, $body)) {
if ($this->isUploadCallAndFilepath($path, $body)) {
@trigger_error('Upload an attachment by filepath is deprecated, use file_get_contents() to upload the file content instead.', E_USER_DEPRECATED);
Art4 marked this conversation as resolved.
Show resolved Hide resolved

$request = $request->withBody(
$this->streamFactory->createStreamFromFile($body)
);
Expand All @@ -203,7 +205,7 @@ private function createRequest(string $method, string $path, string $body = ''):
// set Content-Type header
$tmp = parse_url($this->url.$path);

if (preg_match('/\/uploads.(json|xml)/i', $path)) {
if ($this->isUploadCall($path)) {
$request = $request->withHeader('Content-Type', 'application/octet-stream');
} elseif ('json' === substr($tmp['path'], -4)) {
$request = $request->withHeader('Content-Type', 'application/json');
Expand All @@ -213,13 +215,4 @@ private function createRequest(string $method, string $path, string $body = ''):

return $request;
}

private function isUploadCall(string $path, string $body): bool
{
return
(preg_match('/\/uploads.(json|xml)/i', $path)) &&
'' !== $body &&
is_file(strval(str_replace("\0", '', $body)))
;
}
}