+ /**
+ * Store a upload, saving to a file and deleting any existing uploads
+ * attached to that file.
+ * @param UploadedFile $uploadedFile
+ * @param File $file
+ * @return File
+ * @throws FileUploadException
+ */
+ public function saveUpdatedUpload(UploadedFile $uploadedFile, File $file)
+ {
+ if (!$file->external) {
+ $this->deleteFileInStorage($file);
+ }
+
+ $fileName = $uploadedFile->getClientOriginalName();
+ $filePath = $this->putFileInStorage($fileName, $uploadedFile);
+
+ $file->name = $fileName;
+ $file->path = $filePath;
+ $file->external = false;
+ $file->extension = $uploadedFile->getClientOriginalExtension();
+ $file->save();
+ return $file;
+ }
+
+ /**
+ * Save a new File attachment from a given link and name.
+ * @param string $name
+ * @param string $link
+ * @param int $page_id
+ * @return File
+ */
+ public function saveNewFromLink($name, $link, $page_id)
+ {
+ $largestExistingOrder = File::where('uploaded_to', '=', $page_id)->max('order');
+ return File::forceCreate([
+ 'name' => $name,
+ 'path' => $link,
+ 'external' => true,
+ 'extension' => '',
+ 'uploaded_to' => $page_id,
+ 'created_by' => user()->id,
+ 'updated_by' => user()->id,
+ 'order' => $largestExistingOrder + 1
+ ]);
+ }
+