Commit 50d5da04 authored by Sean Harvey's avatar Sean Harvey

BUG Fixing images not being handled correctly when there's duplicates.

If a document contains duplicates, the existing code for handling
this doesn't work. One of the issues is the renaming of files if
one already exists. As it would turn out, this is not necessary
because LibreOffice names the files with a hash of the file
content anyway, so we'd never run into a case where you'd overwrite
an existing file as they're uniquely named.
e.g. "index-html-ab35fe.jpg".
parent f9a4c1f5
......@@ -247,30 +247,19 @@ class DocumentImportInnerField extends UploadField {
for($i = 0; $i < $imgs->length; $i++) {
$img = $imgs->item($i);
$originalPath = 'assets/' . $folderName . '/' . $img->getAttribute('src');
// get the name of the file, filtered, and check for any existing records
// suffixing them if they already exist (setName() in File won't do this
// for us)
// we do this incase someone is linking to the existing file, we don't
// want to overwrite anything that might be in use!
$info = pathinfo(FileNameFilter::create()->filter(basename($originalPath)));
$name = $info['basename'];
$suffix = 1;
while(DataObject::get_one("File", "\"Name\" = '" . Convert::raw2sql($name)
. "' AND \"ParentID\" = " . (int) $chosenFolderID)) {
$suffix++;
$name = $info['filename'] . '-' . $suffix . '.' . $info['extension'];
$name = FileNameFilter::create()->filter(basename($originalPath));
$image = Image::get()->filter(array('Name' => $name, 'ParentID' => (int) $chosenFolderID))->first();
if(!($image && $image->exists())) {
$image = new Image();
$image->ParentID = (int) $chosenFolderID;
$image->Name = $name;
$image->write();
}
$image = new Image();
$image->ParentID = (int) $chosenFolderID;
$image->Name = $name;
$image->write();
// the filename might've been filtered by File::setName(), so let's make sure the file is moved
// to the place that the Image record expects it to be.
rename(Director::getAbsFile($originalPath), Director::getAbsFile($image->getFilename()));
// make sure it's put in place correctly so Image record knows where it is.
// e.g. in the case of underscores being renamed to dashes.
@rename(Director::getAbsFile($originalPath), Director::getAbsFile($image->getFilename()));
$img->setAttribute('src', $image->getFilename());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment