diff --git a/app/Resources/i18n/en_US/Asset.json b/app/Resources/i18n/en_US/Asset.json index 67ae733..004f0f2 100644 --- a/app/Resources/i18n/en_US/Asset.json +++ b/app/Resources/i18n/en_US/Asset.json @@ -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" } } diff --git a/app/Services/Attachment.php b/app/Services/Attachment.php index 81e198f..6103327 100644 --- a/app/Services/Attachment.php +++ b/app/Services/Attachment.php @@ -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); @@ -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)); }