Merge #1709 썸네일 생성시 race condition 해소 by kijin

* pr/1709:
  Use lockfile instead of empty file
  Prevent race condition on thumbnail generation
This commit is contained in:
Kijin Sung 2015-10-08 15:08:45 +09:00
commit b38b3ed01c
2 changed files with 47 additions and 13 deletions

View file

@ -572,10 +572,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)
{
@ -587,6 +588,9 @@ class commentItem extends Object
}
}
// Create lockfile to prevent race condition
FileHandler::writeFile($thumbnail_lockfile, '', 'w');
// Target file
$source_file = NULL;
$is_tmp_file = FALSE;
@ -677,21 +681,24 @@ 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 not to attempt to generate the thumbnail afterwards
// Create an empty file if thumbnail generation failed
else
{
FileHandler::writeFile($thumbnail_file, '', 'w');
FileHandler::writeFile($thumbnail_file, '','w');
}
return;

View file

@ -828,17 +828,29 @@ class documentItem extends Object
}
$thumbnail_type = $config->thumbnail_type;
}
// Define thumbnail information
$thumbnail_path = sprintf('files/thumbnails/%s',getNumberingPath($this->document_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 thumbnail file exists and its size is 0. Otherwise, return its path
if(file_exists($thumbnail_file))
if(file_exists($thumbnail_file) || file_exists($thumbnail_lockfile))
{
if(filesize($thumbnail_file)<1) return false;
else return $thumbnail_url;
if(filesize($thumbnail_file) < 1)
{
return FALSE;
}
else
{
return $thumbnail_url;
}
}
// Create lockfile to prevent race condition
FileHandler::writeFile($thumbnail_lockfile, '', 'w');
// Target File
$source_file = null;
$is_tmp_file = false;
@ -913,11 +925,26 @@ class documentItem extends Object
{
$output = FileHandler::createImageFile($source_file, $thumbnail_file, $width, $height, 'jpg', $thumbnail_type);
}
if($is_tmp_file) FileHandler::removeFile($source_file);
// Return its path if a thumbnail is successfully genetated
if($output) return $thumbnail_url;
// Create an empty file not to re-generate the thumbnail
else FileHandler::writeFile($thumbnail_file, '','w');
// Remove source file if it was temporary
if($is_tmp_file)
{
FileHandler::removeFile($source_file);
}
// 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;
}