Couple changes to generate_preview().
* Now accepts $force_regen option. * Calling tempfile_path() would cause an error. * Changed file_exists() for is_file().
This commit is contained in:
parent
dde2121e8d
commit
28269e00c6
@ -247,30 +247,48 @@ trait PostFileMethods
|
|||||||
if (!$this->image())
|
if (!$this->image())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// if (type == :sample then) {
|
$force_regen = !empty($options['force_regen']);
|
||||||
// return; false if !generate_sample(options[:force_regen])
|
|
||||||
// temp_path = tempfile_sample_path
|
switch ($type) {
|
||||||
// dest_path = sample_path
|
case 'sample':
|
||||||
// } elseif (type == :jpeg then) {
|
if (!$this->generate_sample($force_regen)) {
|
||||||
// return; false if !generate_jpeg(options[:force_regen])
|
return false;
|
||||||
// temp_path = tempfile_jpeg_path
|
}
|
||||||
// dest_path = jpeg_path
|
$temp_path = $this->tempfile_sample_path();
|
||||||
// } elseif (type == :preview then) {
|
$dest_path = $this->sample_path();
|
||||||
// return; false if !generate_preview
|
break;
|
||||||
// temp_path = tempfile_preview_path
|
|
||||||
// dest_path = preview_path
|
case 'jpeg':
|
||||||
// } else {
|
if (!$this->generate_jpeg($force_regen)) {
|
||||||
// raise Exception, "unknown type: %s" % type
|
return false;
|
||||||
// }
|
}
|
||||||
|
$temp_path = $this->tempfile_jpeg_path();
|
||||||
|
$dest_path = $this->jpeg_path();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'preview':
|
||||||
|
if (!$this->generate_preview($force_regen)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$temp_path = $this->tempfile_preview_path();
|
||||||
|
$dest_path = $this->preview_path();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Exception(sprintf("unknown type: %s", $type));
|
||||||
|
}
|
||||||
|
|
||||||
// # Only move in the changed files on success. When we return; false, the caller won't
|
# Only move in the changed files on success. When we return; false, the caller won't
|
||||||
// # save us to the database; we need to only move the new files in if we're going to be
|
# save us to the database; we need to only move the new files in if we're going to be
|
||||||
// # saved. This is normally handled by move_file.
|
# saved. This is normally handled by move_file.
|
||||||
// if (File.exists?(temp_path)) {
|
if (is_file($temp_path)) {
|
||||||
// FileUtils.mkdir_p(File.dirname(dest_path), 'mode' => 0775)
|
$dest_dir = dirname($dest_path);
|
||||||
// FileUtils.mv(temp_path, dest_path)
|
if (!is_dir($dest_dir)) {
|
||||||
// FileUtils.chmod(0775, dest_path)
|
mkdir($dest_dir, 0775, true);
|
||||||
// }
|
}
|
||||||
|
rename($temp_path, $dest_path);
|
||||||
|
chmod($dest_path, 0775);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -506,21 +524,26 @@ trait PostFileMethods
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generate_preview()
|
protected function generate_preview($force_regen = false)
|
||||||
{
|
{
|
||||||
if (!$this->image() || (!$this->width && !$this->height))
|
if (!$this->image() || (!$this->width && !$this->height))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
# If we already have a preview image, don't regenerate it.
|
||||||
|
if (is_file($this->preview_path()) && !$force_regen) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$size = Moebooru\Resizer::reduce_to(array('width' => $this->width, 'height' => $this->height), array('width' => 300, 'height' => 300));
|
$size = Moebooru\Resizer::reduce_to(array('width' => $this->width, 'height' => $this->height), array('width' => 300, 'height' => 300));
|
||||||
|
|
||||||
# Generate the preview from the new sample if we have one to save CPU, otherwise from the image.
|
# Generate the preview from the new sample if we have one to save CPU, otherwise from the image.
|
||||||
if (file_exists($this->tempfile_sample_path()))
|
if (is_file($this->tempfile_sample_path()))
|
||||||
list($path, $ext) = array($this->tempfile_sample_path(), "jpg");
|
list($path, $ext) = array($this->tempfile_sample_path(), "jpg");
|
||||||
elseif (file_exists($this->sample_path()))
|
elseif (is_file($this->sample_path()))
|
||||||
list($path, $ext) = array($this->sample_path(), "jpg");
|
list($path, $ext) = array($this->sample_path(), "jpg");
|
||||||
elseif (file_exists($this->tempfile_path()))
|
elseif (is_file($this->tempfile_path))
|
||||||
list($path, $ext) = array($this->tempfile_path(), $this->file_ext);
|
list($path, $ext) = array($this->tempfile_path, $this->file_ext);
|
||||||
elseif (file_exists($this->file_path()))
|
elseif (is_file($this->file_path()))
|
||||||
list($path, $ext) = array($this->file_path(), $this->file_ext);
|
list($path, $ext) = array($this->file_path(), $this->file_ext);
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user