Skip to content

Commit

Permalink
Fix up file deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson authored Oct 17, 2020
1 parent 745bf39 commit 165af26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function deletePost(
Request $request,
EntityManagerInterface $entityManager,
PostRepository $postRepository,
Filesystems $filesystems,
string $id
) {
$post = $postRepository->find($id);
$submittedToken = $request->request->get('token');
if ($request->isMethod('post') && $this->isCsrfTokenValid('delete-post', $submittedToken)) {
$filesystems->remove($post->getFile());
$entityManager->remove($post);
$entityManager->flush();
$this->addFlash('success', 'Post deleted.');
Expand Down Expand Up @@ -111,6 +113,7 @@ public function savePost(
$post->setAuthor($author);

$entityManager->persist($post);
$entityManager->flush();

/** @var UploadedFile $newFile */
$newFile = $request->files->get('new_file');
Expand All @@ -120,25 +123,18 @@ public function savePost(
}
$file = $post->getFile() ?? new File();
$file->setPost($post);
$oldExt = $file->getExtension();
$file->setMimeType($newFile->getMimeType());
$file->setSize($newFile->getSize());
$file->setChecksum(sha1_file($newFile->getPathname()));
$entityManager->persist($file);
$post->setFile($file);
$entityManager->persist($post);
$entityManager->flush();
// Delete the old original, in case the extension has changed.
$filepathDataOld = '/files/' . $post->getId() . '.' . $oldExt;
if ($filesystems->data()->has($filepathDataOld)) {
$filesystems->data()->delete($filepathDataOld);
}
// Remove before adding, for replacement files with new extensions.
$filesystems->remove($file);
$filesystems->write($filesystems->data(), $file, $newFile->getPathname());
$filesystems->temp()->deleteDir('files/' . $post->getId() . '/');
}

$entityManager->flush();

$this->addFlash('success', 'Post saved.');
return $this->redirectToRoute('post_view', ['id' => $post->getId()]);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public function write(Filesystem $fs, File $file, string $filePath)
}
}

/**
* Delete a file from both filesystems.
* @param File $file
* @return bool
*/
public function remove(File $file): bool
{
// Delete actual file.
$storagePath = $this->getDataStoragePath($file);
$dataDeleted = true;
if ($this->data()->has($storagePath)) {
$dataDeleted = $this->data()->delete($storagePath);
}
// Delete temp versions.
$tempDeleted = $this->temp()->deleteDir('/files/' . $file->getPost()->getId());
return $dataDeleted && $tempDeleted;
}

/**
* Get a stream of a file, resizing it if requested (in which case it'll always be a JPEG).
* @param File $file The file to fetch.
Expand Down

0 comments on commit 165af26

Please sign in to comment.