/** * Upload file to S3 * * @author Casper Rasmussen <*****@*****.**> * @access protected * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile * @param \Nodes\Assets\Upload\Settings $settings * @return string * @throws \Nodes\Assets\Upload\Exceptions\AssetsUploadFailedException */ protected function store(UploadedFile $uploadedFile, Settings $settings) { try { // Upload to bucket \Storage::disk('s3')->put($settings->getFilePath(), file_get_contents($uploadedFile->getRealPath())); } catch (\Exception $e) { throw new AssetsUploadFailedException('Could not upload file to Amazon S3. Reason: ' . $e->getMessage()); } return $settings->getFilePath(); }
/** * Save file to folder. * * @author Casper Rasmussen <*****@*****.**> * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile * @param \Nodes\Assets\Upload\Settings $settings * * @return string * @throws \Nodes\Assets\Upload\Exceptions\AssetsUploadFailedException */ protected function store(UploadedFile $uploadedFile, Settings $settings) { // Fallback folder is none is set if (!$settings->hasFolder()) { $settings->setFolder('default'); } try { // Retrieve folder path $path = public_path(config('nodes.assets.providers.publicFolder.subFolder')) . DIRECTORY_SEPARATOR . $settings->getFolder(); // If folder doesn't exists, // we'll create it with global permissions if (!file_exists($path)) { mkdir($path, 0777, true); } // Stream uploaded file $content = file_get_contents($uploadedFile->getPathname()); // Save uploaded file to folder $result = file_put_contents($path . DIRECTORY_SEPARATOR . $settings->getFileName() . '.' . $settings->getFileExtension(), $content); if (!$result) { throw new NodesException('Failed to save', 500); } } catch (Exception $e) { throw new AssetsUploadFailedException('Could not save the file to public folder. Reason: ' . $e->getMessage()); } return $settings->getFilePath(); }
/** * addFromDataUri. * * @author Casper Rasmussen <*****@*****.**> * * @param string $dataUri * @param string $folder * @param \Nodes\Assets\Upload\Settings $settings * * @return string * @throws \Nodes\Assets\Upload\Exceptions\AssetsBadRequestException */ public function addFromDataUri($dataUri, $folder, Settings $settings) { // Data URI container $dataUriObject = null; // Try and parse data URI to our container if (!DataUri::tryParse($dataUri, $dataUriObject)) { throw (new AssetsBadRequestException('Could not stream the content'))->setStatusCode(400); } // Retrieve the data $content = $dataUriObject->getEncodedData(); // Write parsed content to temp. file $file = tempnam('/tmp', ''); file_put_contents($file, base64_decode($content)); // File's mime-type $mimeType = (new finfo(FILEINFO_MIME))->file($file); // Generate an UploadedFile object $uploadedFile = new UploadedFile($file, Str::random(10) . '.' . $dataUriObject->getFileExtension(), $mimeType, filesize($file)); // Set folder if (!$settings->hasFolder()) { $settings->setFolder($folder); } // Generate filename if (!$settings->hasFilename()) { $settings->setFileName($this->generateFilename($uploadedFile)); } // Generate file extension if (!$settings->hasFileExtension()) { $settings->setFileExtension($this->generateFileExtension($uploadedFile)); } // Throw exception if required data is missing $settings->checkRequiredData(); // Process file return $this->store($uploadedFile, $settings); }
/** * Retrieve path. * * @author Casper Rasmussen <*****@*****.**> * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile * @param \Nodes\Assets\Upload\Settings $settings * * @return string */ private function getPath(UploadedFile $uploadedFile, Settings $settings) { return env('APP_NAME') . DIRECTORY_SEPARATOR . $this->getSubFolder($uploadedFile) . DIRECTORY_SEPARATOR . $settings->getFolder(); }