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

View file

@ -818,19 +818,28 @@ class documentItem extends Object
} }
$thumbnail_type = $config->thumbnail_type; $thumbnail_type = $config->thumbnail_type;
} }
// Define thumbnail information // Define thumbnail information
$thumbnail_path = sprintf('files/thumbnails/%s',getNumberingPath($this->document_srl, 3)); $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_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; $thumbnail_url = Context::getRequestUri().$thumbnail_file;
// Return false if thumbnail file exists and its size is 0. Otherwise, return its path // 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; if(filesize($thumbnail_file) < 1)
else return $thumbnail_url; {
return FALSE;
}
else
{
return $thumbnail_url;
}
} }
// Prevent race condition // Create lockfile to prevent race condition
FileHandler::writeFile($thumbnail_file, '', 'w'); FileHandler::writeFile($thumbnail_lockfile, '', 'w');
// Target File // Target File
$source_file = null; $source_file = null;
@ -906,9 +915,26 @@ class documentItem extends Object
{ {
$output = FileHandler::createImageFile($source_file, $thumbnail_file, $width, $height, 'jpg', $thumbnail_type); $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 // Remove source file if it was temporary
if($output) return $thumbnail_url; 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; return;
} }