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
1 changed files with 141 additions and 125 deletions

View File

@ -318,63 +318,70 @@ class PhotoController extends Controller
}
else
{
if ($request->has('photo_id'))
try
{
// 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;
if ($request->has('photo_id'))
{
// 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);
$uploadedTempFile = new File($photoFile);
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid, $photo->storage_file_name);
$uploadedTempFile = new File($photoFile);
$this->removeExistingActivityRecords($photo, 'photo.uploaded');
$this->removeExistingActivityRecords($photo, 'photo.taken');
$this->removeExistingActivityRecords($photo, 'photo.uploaded');
$this->removeExistingActivityRecords($photo, 'photo.taken');
$photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $uploadedTempFile->getSize();
$photo->storage_file_name = basename($queuedFileName);
$photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $uploadedTempFile->getSize();
$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);
$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);
@unlink($photoFile->getRealPath());
}
$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());
@mkdir($temporaryFolder);
$queueStorage = AnalysisQueueHelper::getStorageQueueSource();
$mimeType = strtolower($archiveFile->getMimeType());
switch ($mimeType)
try
{
case 'application/zip':
$zip = new \ZipArchive();
$zip->open($archiveFile->getPathname());
$zip->extractTo($temporaryFolder);
$zip->close();
@unlink($archiveFile->getPathname());
break;
$queueStorage = AnalysisQueueHelper::getStorageQueueSource();
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())
$mimeType = strtolower($archiveFile->getMimeType());
switch ($mimeType)
{
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
@unlink($fileInfo->getPathname());
continue;
}
$result = getimagesize($fileInfo->getPathname());
if ($result === false)
{
// Not an image file - skip
@unlink($fileInfo->getPathname());
continue;
}
$photoFile = new File($fileInfo->getPathname());
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->user_id = Auth::user()->id;
$photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME);
$photo->file_name = $photoFile->getFilename();
$photo->mime_type = $photoFile->getMimeType();
$photo->file_size = $photoFile->getSize();
$photo->is_analysed = false;
$photo->storage_file_name = basename($queuedFileName);
$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);
if (substr($fileInfo->getFilename(), 0, 1) == '.')
{
// Temporary/hidden file - skip
@unlink($fileInfo->getRealPath());
continue;
}
$result = getimagesize($fileInfo->getRealPath());
if ($result === false)
{
// Not an image file - skip
@unlink($fileInfo->getRealPath());
continue;
}
$photoFile = new File($fileInfo->getRealPath());
$queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->user_id = Auth::user()->id;
$photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME);
$photo->file_name = $photoFile->getFilename();
$photo->mime_type = $photoFile->getMimeType();
$photo->file_size = $photoFile->getSize();
$photo->is_analysed = false;
$photo->storage_file_name = basename($queuedFileName);
$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);
}
@unlink($fileInfo->getRealPath());
}
}
finally
{
@rmdir($temporaryFolder);
}
return redirect(route('albums.analyse', [
'id' => $album->id,