1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

Added basic attachment editing functionality

This commit is contained in:
Dan Brown
2016-10-11 20:39:11 +01:00
parent 89509b487a
commit 867fc8be64
7 changed files with 307 additions and 79 deletions

View File

@@ -57,6 +57,70 @@ class FileController extends Controller
return response()->json($file);
}
/**
* Update an uploaded file.
* @param int $fileId
* @param Request $request
* @return mixed
*/
public function uploadUpdate($fileId, Request $request)
{
$this->validate($request, [
'uploaded_to' => 'required|integer|exists:pages,id',
'file' => 'required|file'
]);
$pageId = $request->get('uploaded_to');
$page = $this->pageRepo->getById($pageId);
$file = $this->file->findOrFail($fileId);
$this->checkOwnablePermission('page-update', $page);
$this->checkOwnablePermission('file-create', $file);
if (intval($pageId) !== intval($file->uploaded_to)) {
return $this->jsonError('Page mismatch during attached file update');
}
$uploadedFile = $request->file('file');
try {
$file = $this->fileService->saveUpdatedUpload($uploadedFile, $file);
} catch (FileUploadException $e) {
return response($e->getMessage(), 500);
}
return response()->json($file);
}
/**
* Update the details of an existing file.
* @param $fileId
* @param Request $request
* @return File|mixed
*/
public function update($fileId, Request $request)
{
$this->validate($request, [
'uploaded_to' => 'required|integer|exists:pages,id',
'name' => 'string|max:255',
'link' => 'url'
]);
$pageId = $request->get('uploaded_to');
$page = $this->pageRepo->getById($pageId);
$file = $this->file->findOrFail($fileId);
$this->checkOwnablePermission('page-update', $page);
$this->checkOwnablePermission('file-create', $file);
if (intval($pageId) !== intval($file->uploaded_to)) {
return $this->jsonError('Page mismatch during attachment update');
}
$file = $this->fileService->updateFile($file, $request->all());
return $file;
}
/**
* Attach a link to a page as a file.
* @param Request $request
@@ -66,8 +130,8 @@ class FileController extends Controller
{
$this->validate($request, [
'uploaded_to' => 'required|integer|exists:pages,id',
'name' => 'string',
'link' => 'url'
'name' => 'string|max:255',
'link' => 'url|max:255'
]);
$pageId = $request->get('uploaded_to');

View File

@@ -32,26 +32,7 @@ class FileService extends UploadService
public function saveNewUpload(UploadedFile $uploadedFile, $page_id)
{
$fileName = $uploadedFile->getClientOriginalName();
$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.');
}
$filePath = $this->putFileInStorage($fileName, $uploadedFile);
$largestExistingOrder = File::where('uploaded_to', '=', $page_id)->max('order');
$file = File::forceCreate([
@@ -66,6 +47,30 @@ class FileService extends UploadService
return $file;
}
/**
* 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->save();
return $file;
}
/**
* Save a new File attachment from a given link and name.
* @param string $name
@@ -109,8 +114,29 @@ class FileService extends UploadService
}
}
/**
* Delete a file and any empty folders the deletion leaves.
* Update the details of a file.
* @param File $file
* @param $requestData
* @return File
*/
public function updateFile(File $file, $requestData)
{
$file->name = $requestData['name'];
if (isset($requestData['link']) && trim($requestData['link']) !== '') {
$file->path = $requestData['link'];
if (!$file->external) {
$this->deleteFileInStorage($file);
$file->external = true;
}
}
$file->save();
return $file;
}
/**
* Delete a File from the database and storage.
* @param File $file
*/
public function deleteFile(File $file)
@@ -120,6 +146,17 @@ class FileService extends UploadService
return;
}
$this->deleteFileInStorage($file);
$file->delete();
}
/**
* Delete a file from the filesystem it sits on.
* Cleans any empty leftover folders.
* @param File $file
*/
protected function deleteFileInStorage(File $file)
{
$storedFilePath = $this->getStorageBasePath() . $file->path;
$storage = $this->getStorage();
$dirPath = dirname($storedFilePath);
@@ -128,8 +165,37 @@ class FileService extends UploadService
if (count($storage->allFiles($dirPath)) === 0) {
$storage->deleteDirectory($dirPath);
}
}
$file->delete();
/**
* 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;
}
}