Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #134 from atrocore/i-j65b78a4ec641e8c5
Browse files Browse the repository at this point in the history
Fix http error recognition on asset creation by url
  • Loading branch information
rratsun authored Jan 30, 2024
2 parents bdda8d6 + 68550a3 commit 025630d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/Resources/i18n/en_US/Asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"assetIsAlreadyLinkedWithEntity": "Entity 'Asset' is already linked with entity '%s'. You can't link these entities again.",
"fileNameNotValid": "Filename is not valid.",
"suchFileNameNotValid": "Filename '%s' is not valid.",
"fileNameNotValidByUserRegex": "Filename is not valid. It should match the regex '%s'."
"fileNameNotValidByUserRegex": "Filename is not valid. It should match the regex '%s'.",
"fileResourceWriteFailed": "Can't write any data to the file %s"
}
}
8 changes: 7 additions & 1 deletion app/Services/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function createEntityByUrl(string $url, bool $validateAttachment = true):
// load file from url
set_time_limit(0);
$fp = fopen($attachment->fileName, 'w+');
if ($fp === false) {
throw new Error(sprintf($this->getInjection('language')->translate('fileResourceWriteFailed', 'exceptions', 'Asset'), $attachment->name));
}
$ch = curl_init(str_replace(" ", "%20", $url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
Expand All @@ -94,7 +97,10 @@ public function createEntityByUrl(string $url, bool $validateAttachment = true):
curl_close($ch);
fclose($fp);

if (!empty($responseCode) && !in_array($responseCode, [200, 201]) || !file_exists($attachment->fileName)) {
if (!in_array($responseCode, [200, 201])) {
if (file_exists($attachment->fileName)) {
unlink($attachment->fileName);
}
throw new Error(sprintf($this->getInjection('language')->translate('urlDownloadFailed', 'exceptions', 'Asset'), $url));
}

Expand Down

0 comments on commit 025630d

Please sign in to comment.