+ /**
+ * Store a file in storage with the given filename
+ * @param $fileName
+ * @param UploadedFile $uploadedFile
+ * @return string
+ * @throws FileUploadException
+ */
+ protected function putFileInStorage($fileName, UploadedFile $uploadedFile)
+ {
+ $fileData = file_get_contents($uploadedFile->getRealPath());
+
+ $storage = $this->getStorage();
+ $fileBasePath = 'uploads/files/' . Date('Y-m-M') . '/';
+ $storageBasePath = $this->getStorageBasePath() . $fileBasePath;
+
+ $uploadFileName = $fileName;
+ while ($storage->exists($storageBasePath . $uploadFileName)) {
+ $uploadFileName = str_random(3) . $uploadFileName;
+ }
+
+ $filePath = $fileBasePath . $uploadFileName;
+ $fileStoragePath = $this->getStorageBasePath() . $filePath;
+
+ try {
+ $storage->put($fileStoragePath, $fileData);
+ } catch (Exception $e) {
+ throw new FileUploadException('File path ' . $fileStoragePath . ' could not be uploaded to. Ensure it is writable to the server.');
+ }
+ return $filePath;