";
if ($originals_separate_storage) {
if ($originals_separate_storage_ffmpegalts_as_previews && $ffmpeg_alt) {
// we have to consider the fact that this may be in either location
if (strpos($path_trim, '/original') === 0) {
$path_trim = substr($path_trim, 9);
} elseif (strpos($path_trim, '/resized') === 0) {
$path_trim = substr($path_trim, 8);
}
}
// take the separator out of the path
elseif ($size == '' || $size == 'o') {
$path_trim = substr($path_trim, 9);
} else {
$path_trim = substr($path_trim, 8);
}
echo "Removed path part:$path_trim
";
} else {
// add the separator into the path
if ($originals_separate_storage_ffmpegalts_as_previews) {
if ($ffmpeg_alt) {
$path_trim = "/resized" . $path_trim;
} else {
$path_trim = "/original" . $path_trim;
}
} elseif ($size == '' || $size == 'o') {
$path_trim = "/original" . $path_trim;
} else {
$path_trim = "/resized" . $path_trim;
}
echo "Added path part:$path_trim
";
}
return $remove . $path_trim;
}
function filestore_relocate($from, $to)
{
$filepath = $to;
$otherpath = $from;
$file_dir = explode("/", $filepath);
array_pop($file_dir);
$file_dir = implode("/", $file_dir);
echo "Copying file to proper location: $file_dir
";
if (!file_exists($file_dir)) {
echo "Need to make directory first...";
@mkdir($file_dir, 0777, true);
chmod($file_dir, 0777);
echo "done!
";
}
if (!copy($otherpath, $filepath)) {
echo "Failed to copy file...skipping
";
} else {
echo "Copy complete!
";
// remove the file
unlink($otherpath);
}
}
if ($refs == '') {
# start with a list of all resources
$refs = ps_array("select ref value from resource where ref>0 order by ref");
} else {
$refs = explode(",", $refs);
}
// check for the presence of the separation folders in filestore
if (!file_exists($storagedir . "/original") && $originals_separate_storage) {
echo "Original directory not present in filestore...making...";
@mkdir($storagedir . "/original");
chmod($storagedir . "/original", 0777);
echo "done!
";
}
if (!file_exists($storagedir . "/resized") && $originals_separate_storage) {
echo "Resized directory not present in filestore...making...";
@mkdir($storagedir . "/resized");
chmod($storagedir . "/resized", 0777);
echo "done!
";
}
foreach ($refs as $ref) {
$resource_data = get_resource_data($ref);
# get the current filepath of the original based on the current setting of $originals_separate_storage
$filepath = get_resource_path($ref, true, '', false, $resource_data['file_extension']);
# also get the other possible path
$otherpath = reverse_filestore_location($filepath, '');
echo"Filepath:";
print_r($filepath);
echo"
";
if (file_exists($filepath)) {
// original exists where it should
echo "Original file found in proper location
";
// if the file also exists in the old location delete it
if (file_exists($otherpath)) {
// remove the file
unlink($otherpath);
}
} else {
// original needs to be moved
echo "Original file not found in proper location
";
// test for the presense of the file in the alternate location
echo "Other path:$otherpath
";
if (file_exists($otherpath)) {
// let's move it to where it belongs. start by trimming the filename off the path
filestore_relocate($otherpath, $filepath);
} else {
echo "No original file found!
";
}
}
// now we need to deal with the other files...start with alternatives
echo "Checking for alternative files...";
$alts = get_alternative_files($ref);
if (!empty($alts)) {
echo "alts found!
";
// these get moved to originals
foreach ($alts as $alt) {
echo "Alt:";
print_r($alt);
echo"
";
$ffmpeg_alt = alt_is_ffmpeg_alternative($alt);
$alt_filepath = get_resource_path($ref, true, '', false, $alt['file_extension'], -1, 1, false, '', $alt["ref"]);
if ($ffmpeg_alt) {
if (strpos($alt_filepath, '/original/') !== false) {
$ffmpeg_alt_filepath = str_replace('/original/', '/resized/', $alt_filepath);
} elseif (strpos($alt_filepath, '/resized/') !== false) {
$ffmpeg_alt_filepath = str_replace('/resized/', '/original/', $alt_filepath);
}
if (isset($ffmpeg_alt_filepath)) {
echo 'ffmpeg_alt_filepath=' . $ffmpeg_alt_filepath . '
';
}
}
echo 'Alt Filepath:' . $alt_filepath . '
';
$alt_otherpath = reverse_filestore_location($alt_filepath, '', false, $ffmpeg_alt);
if (file_exists($alt_filepath)) {
echo "Alt file " . $alt["ref"] . " found in proper location
";
if (file_exists($alt_otherpath)) {
// remove the file
unlink($alt_otherpath);
}
} else {
echo "Alt file " . $alt["ref"] . " not found in proper location
";
if (file_exists($alt_otherpath)) {
// let's move it to where it belongs. start by trimming the filename off the path
filestore_relocate($alt_otherpath, $alt_filepath);
} elseif ($ffmpeg_alt && isset($ffmpeg_alt_filepath) && file_exists($ffmpeg_alt_filepath)) {
echo "Alt file is ffmpeg_alt in old setting location
";
filestore_relocate($ffmpeg_alt_filepath, $alt_filepath);
} else {
echo "Alternative file not found!
";
}
}
}
} else {
echo "none found
";
}
// finally, move everything else in the directory
echo "Checking for previews...";
$other_dir = explode("/", $otherpath);
array_pop($other_dir);
$other_dir = implode("/", $other_dir);
echo $storagedir . "/original
";
if (!$originals_separate_storage && strpos($other_dir, $storagedir . "/original") !== false) {
echo "replacing...";
$other_dir = str_replace($storagedir . "/original", $storagedir . "/resized", $other_dir);
}
echo "Other dir=$other_dir
";
// get a list of what's left:
if (file_exists($other_dir)) {
$previews = array_diff(scandir($other_dir), array('..', '.'));
echo "Previews:";
print_r($previews);
echo"
";
if (!empty($previews)) {
echo "previews found!
";
// grab any preview filepath
$template_path = get_resource_path($ref, true, 'pre', false, 'jpg');
$template_otherpath = reverse_filestore_location($template_path, 'pre');
$file_dir = explode("/", $template_path);
array_pop($file_dir);
$file_dir = implode("/", $file_dir);
$other_dir = explode("/", $template_otherpath);
array_pop($other_dir);
$other_dir = implode("/", $other_dir);
foreach ($previews as $preview) {
$preview_filepath = $file_dir . "/" . $preview;
$preview_otherpath = $other_dir . "/" . $preview;
if (file_exists($preview_filepath)) {
echo "Preview " . $preview . " found in proper location
";
if (file_exists($preview_otherpath)) {
unlink($preview_otherpath);
}
} else {
echo "Preview " . $preview . " not found in proper location
";
if (file_exists($preview_otherpath)) {
echo "Moving $preview...";
filestore_relocate($preview_otherpath, $preview_filepath);
} else {
echo "Preview not found!
";
}
}
}
} else {
echo "no previews found!
";
}
} else {
echo "no previews directory found!
";
}
}
echo "Move complete!
";
if ($cleanup) {
// get rid of the old directories...this will only be implemented when we're sure the script works flawlessly
}