Remove temporary files once they're in the analysis queue following an upload

This commit is contained in:
Andy Heathershaw 2019-07-27 14:05:19 +01:00
parent f4f4de1b34
commit 24f2155a35

View File

@ -318,63 +318,70 @@ class PhotoController extends Controller
} }
else else
{ {
if ($request->has('photo_id')) try
{ {
// Photo ID provided (using the Replace Photo function) - use that record if ($request->has('photo_id'))
$photo = Photo::where('id', intval($request->get('photo_id')))->first(); {
$photo->raw_exif_data = null; // Photo ID provided (using the Replace Photo function) - use that record
$photo = Photo::where('id', intval($request->get('photo_id')))->first();
$photo->raw_exif_data = null;
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid, $photo->storage_file_name); $queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid, $photo->storage_file_name);
$uploadedTempFile = new File($photoFile); $uploadedTempFile = new File($photoFile);
$this->removeExistingActivityRecords($photo, 'photo.uploaded'); $this->removeExistingActivityRecords($photo, 'photo.uploaded');
$this->removeExistingActivityRecords($photo, 'photo.taken'); $this->removeExistingActivityRecords($photo, 'photo.taken');
$photo->file_name = $photoFile->getClientOriginalName(); $photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $uploadedTempFile->getMimeType(); $photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $uploadedTempFile->getSize(); $photo->file_size = $uploadedTempFile->getSize();
$photo->storage_file_name = basename($queuedFileName); $photo->storage_file_name = basename($queuedFileName);
}
else
{
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid);
$uploadedTempFile = new File($photoFile);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->user_id = Auth::user()->id;
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
$photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $uploadedTempFile->getSize();
$photo->storage_file_name = basename($queuedFileName);
}
$photo->is_analysed = false;
$photo->save();
// Log an activity record for the user's feed
$this->createActivityRecord($photo, 'photo.uploaded');
// If queueing is enabled, store the photo in the queue now
if (UserConfig::isImageProcessingQueueEnabled())
{
$queueItem = new QueueItem([
'batch_reference' => $queueUid,
'action_type' => 'photo.analyse',
'album_id' => $photo->album_id,
'photo_id' => $photo->id,
'user_id' => $this->getUser()->id,
'queued_at' => new \DateTime()
]);
$queueItem->save();
$rabbitmq = new RabbitMQService();
$rabbitmq->queueItem($queueItem);
}
$isSuccessful = true;
} }
else finally
{ {
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid); @unlink($photoFile->getRealPath());
$uploadedTempFile = new File($photoFile);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->user_id = Auth::user()->id;
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
$photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $uploadedTempFile->getSize();
$photo->storage_file_name = basename($queuedFileName);
} }
$photo->is_analysed = false;
$photo->save();
// Log an activity record for the user's feed
$this->createActivityRecord($photo, 'photo.uploaded');
// If queueing is enabled, store the photo in the queue now
if (UserConfig::isImageProcessingQueueEnabled())
{
$queueItem = new QueueItem([
'batch_reference' => $queueUid,
'action_type' => 'photo.analyse',
'album_id' => $photo->album_id,
'photo_id' => $photo->id,
'user_id' => $this->getUser()->id,
'queued_at' => new \DateTime()
]);
$queueItem->save();
$rabbitmq = new RabbitMQService();
$rabbitmq->queueItem($queueItem);
}
$isSuccessful = true;
} }
} }
@ -422,89 +429,98 @@ class PhotoController extends Controller
$temporaryFolder = sprintf('%s/%s', sys_get_temp_dir(), MiscHelper::randomString()); $temporaryFolder = sprintf('%s/%s', sys_get_temp_dir(), MiscHelper::randomString());
@mkdir($temporaryFolder); @mkdir($temporaryFolder);
$queueStorage = AnalysisQueueHelper::getStorageQueueSource(); try
$mimeType = strtolower($archiveFile->getMimeType());
switch ($mimeType)
{ {
case 'application/zip': $queueStorage = AnalysisQueueHelper::getStorageQueueSource();
$zip = new \ZipArchive();
$zip->open($archiveFile->getPathname());
$zip->extractTo($temporaryFolder);
$zip->close();
@unlink($archiveFile->getPathname());
break;
default: $mimeType = strtolower($archiveFile->getMimeType());
$request->session()->flash('error', sprintf('The file type "%s" is not supported for bulk uploads.', $mimeType)); switch ($mimeType)
return redirect(route('albums.show', ['id' => $album->id]));
}
$di = new \RecursiveDirectoryIterator($temporaryFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
$recursive = new \RecursiveIteratorIterator($di);
/** @var \SplFileInfo $fileInfo */
foreach ($recursive as $fileInfo)
{
if ($fileInfo->isDir())
{ {
if ($fileInfo->getFilename() == '__MACOSX' || substr($fileInfo->getFilename(), 0, 1) == '.') case 'application/zip':
$zip = new \ZipArchive();
$zip->open($archiveFile->getPathname());
$zip->extractTo($temporaryFolder);
$zip->close();
@unlink($archiveFile->getPathname());
break;
default:
$request->session()->flash('error', sprintf('The file type "%s" is not supported for bulk uploads.', $mimeType));
return redirect(route('albums.show', ['id' => $album->id]));
}
$di = new \RecursiveDirectoryIterator($temporaryFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
$recursive = new \RecursiveIteratorIterator($di);
/** @var \SplFileInfo $fileInfo */
foreach ($recursive as $fileInfo)
{
if ($fileInfo->isDir())
{ {
@rmdir($fileInfo->getPathname()); if ($fileInfo->getFilename() == '__MACOSX' || substr($fileInfo->getFilename(), 0, 1) == '.')
{
@rmdir($fileInfo->getRealPath());
}
continue;
} }
continue; if (substr($fileInfo->getFilename(), 0, 1) == '.')
} {
// Temporary/hidden file - skip
if (substr($fileInfo->getFilename(), 0, 1) == '.') @unlink($fileInfo->getRealPath());
{ continue;
// Temporary/hidden file - skip }
@unlink($fileInfo->getPathname());
continue; $result = getimagesize($fileInfo->getRealPath());
} if ($result === false)
{
$result = getimagesize($fileInfo->getPathname()); // Not an image file - skip
if ($result === false) @unlink($fileInfo->getRealPath());
{ continue;
// Not an image file - skip }
@unlink($fileInfo->getPathname());
continue; $photoFile = new File($fileInfo->getRealPath());
} $queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid);
$photoFile = new File($fileInfo->getPathname()); $photo = new Photo();
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid); $photo->album_id = $album->id;
$photo->user_id = Auth::user()->id;
$photo = new Photo(); $photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME);
$photo->album_id = $album->id; $photo->file_name = $photoFile->getFilename();
$photo->user_id = Auth::user()->id; $photo->mime_type = $photoFile->getMimeType();
$photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME); $photo->file_size = $photoFile->getSize();
$photo->file_name = $photoFile->getFilename(); $photo->is_analysed = false;
$photo->mime_type = $photoFile->getMimeType(); $photo->storage_file_name = basename($queuedFileName);
$photo->file_size = $photoFile->getSize(); $photo->save();
$photo->is_analysed = false;
$photo->storage_file_name = basename($queuedFileName); // Log an activity record for the user's feed
$photo->save(); $this->createActivityRecord($photo, 'photo.uploaded');
// Log an activity record for the user's feed // If queueing is enabled, store the photo in the queue now
$this->createActivityRecord($photo, 'photo.uploaded'); if (UserConfig::isImageProcessingQueueEnabled())
{
// If queueing is enabled, store the photo in the queue now $queueItem = new QueueItem([
if (UserConfig::isImageProcessingQueueEnabled()) 'batch_reference' => $queueUid,
{ 'action_type' => 'photo.analyse',
$queueItem = new QueueItem([ 'album_id' => $photo->album_id,
'batch_reference' => $queueUid, 'photo_id' => $photo->id,
'action_type' => 'photo.analyse', 'user_id' => $this->getUser()->id,
'album_id' => $photo->album_id, 'queued_at' => new \DateTime()
'photo_id' => $photo->id, ]);
'user_id' => $this->getUser()->id, $queueItem->save();
'queued_at' => new \DateTime()
]); $rabbitmq = new RabbitMQService();
$queueItem->save(); $rabbitmq->queueItem($queueItem);
}
$rabbitmq = new RabbitMQService();
$rabbitmq->queueItem($queueItem); @unlink($fileInfo->getRealPath());
} }
} }
finally
{
@rmdir($temporaryFolder);
}
return redirect(route('albums.analyse', [ return redirect(route('albums.analyse', [
'id' => $album->id, 'id' => $album->id,