Use lockfile instead of empty file

This commit is contained in:
Kijin Sung 2015-09-17 16:35:34 +09:00
parent 8d92b0eeb9
commit 91b0680c21
2 changed files with 48 additions and 12 deletions

View file

@ -569,10 +569,11 @@ class commentItem extends Object
// Define thumbnail information
$thumbnail_path = sprintf('files/thumbnails/%s', getNumberingPath($this->comment_srl, 3));
$thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, $width, $height, $thumbnail_type);
$thumbnail_lockfile = sprintf('%s%dx%d.%s.lock', $thumbnail_path, $width, $height, $thumbnail_type);
$thumbnail_url = Context::getRequestUri() . $thumbnail_file;
// return false if a size of existing thumbnail file is 0. otherwise return the file path
if(file_exists($thumbnail_file))
if(file_exists($thumbnail_file) || file_exists($thumbnail_lockfile))
{
if(filesize($thumbnail_file) < 1)
{
@ -584,8 +585,8 @@ class commentItem extends Object
}
}
// Prevent race condition
FileHandler::writeFile($thumbnail_file, '', 'w');
// Create lockfile to prevent race condition
FileHandler::writeFile($thumbnail_lockfile, '', 'w');
// Target file
$source_file = NULL;
@ -677,16 +678,25 @@ class commentItem extends Object
$output = FileHandler::createImageFile($source_file, $thumbnail_file, $width, $height, 'jpg', $thumbnail_type);
// Remove source file if it was temporary
if($is_tmp_file)
{
FileHandler::removeFile($source_file);
}
// return the thumbnail path if successfully generated.
// Remove lockfile
FileHandler::removeFile($thumbnail_lockfile);
// Return the thumbnail path if it was successfully generated
if($output)
{
return $thumbnail_url;
}
// Create an empty file if thumbnail generation failed
else
{
FileHandler::writeFile($thumbnail_file, '','w');
}
return;
}