function testWritingSubsiteID() { $this->objFromFixture('Member', 'admin')->logIn(); $subsite = $this->objFromFixture('Subsite', 'domaintest1'); FileSubsites::$default_root_folders_global = true; Subsite::changeSubsite(0); $file = new File(); $file->write(); $file->onAfterUpload(); $this->assertEquals((int) $file->SubsiteID, 0); Subsite::changeSubsite($subsite->ID); $this->assertTrue($file->canEdit()); $file = new File(); $file->write(); $this->assertEquals((int) $file->SubsiteID, 0); $this->assertTrue($file->canEdit()); FileSubsites::$default_root_folders_global = false; Subsite::changeSubsite($subsite->ID); $file = new File(); $file->write(); $this->assertEquals($file->SubsiteID, $subsite->ID); // Test inheriting from parent folder $folder = new Folder(); $folder->write(); $this->assertEquals($folder->SubsiteID, $subsite->ID); FileSubsites::$default_root_folders_global = true; $file = new File(); $file->ParentID = $folder->ID; $file->onAfterUpload(); $this->assertEquals($folder->SubsiteID, $file->SubsiteID); }
/** * Save an file passed from a form post into this object. * File names are filtered through {@link FileNameFilter}, see class documentation * on how to influence this behaviour. * * @param $tmpFile array Indexed array that PHP generated for every file it uploads. * @param $folderPath string Folder path relative to /assets * @return Boolean|string Either success or error-message. */ public function load($tmpFile, $folderPath = false) { $this->clearErrors(); if (!$folderPath) { $folderPath = $this->config()->uploads_folder; } if (!is_array($tmpFile)) { user_error("Upload::load() Not passed an array. Most likely, the form hasn't got the right enctype", E_USER_ERROR); } if (!$tmpFile['size']) { $this->errors[] = _t('File.NOFILESIZE', 'Filesize is zero bytes.'); return false; } $valid = $this->validate($tmpFile); if (!$valid) { return false; } // @TODO This puts a HUGE limitation on files especially when lots // have been uploaded. $base = Director::baseFolder(); $parentFolder = Folder::find_or_make($folderPath); // Generate default filename $nameFilter = FileNameFilter::create(); $file = $nameFilter->filter($tmpFile['name']); $fileName = basename($file); $relativeFolderPath = $parentFolder ? $parentFolder->getRelativePath() : ASSETS_DIR . '/'; $relativeFilePath = $relativeFolderPath . $fileName; // Create a new file record (or try to retrieve an existing one) if (!$this->file) { $fileClass = File::get_class_for_file_extension(pathinfo($tmpFile['name'], PATHINFO_EXTENSION)); $this->file = new $fileClass(); } if (!$this->file->ID && $this->replaceFile) { $fileClass = $this->file->class; $file = File::get()->filter(array('ClassName' => $fileClass, 'Name' => $fileName, 'ParentID' => $parentFolder ? $parentFolder->ID : 0))->First(); if ($file) { $this->file = $file; } } // if filename already exists, version the filename (e.g. test.gif to test2.gif, test2.gif to test3.gif) if (!$this->replaceFile) { $fileSuffixArray = explode('.', $fileName); $fileTitle = array_shift($fileSuffixArray); $fileSuffix = !empty($fileSuffixArray) ? '.' . implode('.', $fileSuffixArray) : null; // make sure files retain valid extensions $oldFilePath = $relativeFilePath; $relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix; if ($oldFilePath !== $relativeFilePath) { user_error("Couldn't fix {$relativeFilePath}", E_USER_ERROR); } while (file_exists("{$base}/{$relativeFilePath}")) { $i = isset($i) ? $i + 1 : 2; $oldFilePath = $relativeFilePath; $pattern = '/([0-9]+$)/'; if (preg_match($pattern, $fileTitle)) { $fileTitle = preg_replace($pattern, $i, $fileTitle); } else { $fileTitle .= $i; } $relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix; if ($oldFilePath == $relativeFilePath && $i > 2) { user_error("Couldn't fix {$relativeFilePath} with {$i} tries", E_USER_ERROR); } } } else { //reset the ownerID to the current member when replacing files $this->file->OwnerID = Member::currentUser() ? Member::currentUser()->ID : 0; } if (file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], "{$base}/{$relativeFilePath}")) { $this->file->ParentID = $parentFolder ? $parentFolder->ID : 0; // This is to prevent it from trying to rename the file $this->file->Name = basename($relativeFilePath); $this->file->write(); $this->file->onAfterUpload(); $this->extend('onAfterLoad', $this->file); //to allow extensions to e.g. create a version after an upload return true; } else { $this->errors[] = _t('File.NOFILESIZE', 'Filesize is zero bytes.'); return false; } }
public function onAfterUpload() { $this->deleteFormattedImages(); parent::onAfterUpload(); }