public function setUp() { parent::setUp(); $this->logInWithPermission('ADMIN'); // Save versioned state $this->oldReadingMode = Versioned::get_reading_mode(); Versioned::set_stage(Versioned::DRAFT); // Set backend root to /UploadFieldTest AssetStoreTest_SpyStore::activate('UploadFieldTest'); // Set the File Name Filter replacements so files have the expected names Config::inst()->update('SilverStripe\\Assets\\FileNameFilter', 'default_replacements', array('/\\s/' => '-', '/_/' => '-', '/[^A-Za-z0-9+.\\-]+/' => '', '/[\\-]{2,}/' => '-', '/^[\\.\\-_]+/' => '')); // Create a test folders for each of the fixture references foreach (Folder::get() as $folder) { $path = AssetStoreTest_SpyStore::getLocalPath($folder); Filesystem::makeFolder($path); } // Create a test files for each of the fixture references $files = File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder'); foreach ($files as $file) { $path = AssetStoreTest_SpyStore::getLocalPath($file); Filesystem::makeFolder(dirname($path)); $fh = fopen($path, "w+"); fwrite($fh, str_repeat('x', 1000000)); fclose($fh); } }
public function setUp() { parent::setUp(); // Set backend root to /ImageTest AssetStoreTest_SpyStore::activate('ProtectedFileControllerTest'); // Create a test folders for each of the fixture references foreach (Folder::get() as $folder) { /** @var Folder $folder */ $filePath = AssetStoreTest_SpyStore::getLocalPath($folder); Filesystem::makeFolder($filePath); } // Create a test files for each of the fixture references foreach (File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder') as $file) { /** @var File $file */ $path = AssetStoreTest_SpyStore::getLocalPath($file); Filesystem::makeFolder(dirname($path)); $fh = fopen($path, "w+"); fwrite($fh, str_repeat('x', 1000000)); fclose($fh); // Create variant for each file $this->getAssetStore()->setFromString(str_repeat('y', 100), $file->Filename, $file->Hash, 'variant'); } }
/** * Get the children of this folder that are also folders. * * @return DataList */ public function ChildFolders() { return Folder::get()->filter('ParentID', $this->ID); }
/** * Creates a single file based on a form-urlencoded upload. * * @param HTTPRequest $request * @return HTTPRequest|HTTPResponse */ public function apiCreateFile(HTTPRequest $request) { $data = $request->postVars(); $upload = $this->getUpload(); // CSRF check $token = SecurityToken::inst(); if (empty($data[$token->getName()]) || !$token->check($data[$token->getName()])) { return new HTTPResponse(null, 400); } // Check parent record /** @var Folder $parentRecord */ $parentRecord = null; if (!empty($data['ParentID']) && is_numeric($data['ParentID'])) { $parentRecord = Folder::get()->byID($data['ParentID']); } $data['Parent'] = $parentRecord; $tmpFile = $request->postVar('Upload'); if (!$upload->validate($tmpFile)) { $result = ['message' => null]; $errors = $upload->getErrors(); if ($message = array_shift($errors)) { $result['message'] = ['type' => 'error', 'value' => $message]; } return (new HTTPResponse(json_encode($result), 400))->addHeader('Content-Type', 'application/json'); } // TODO Allow batch uploads $fileClass = File::get_class_for_file_extension(File::get_file_extension($tmpFile['name'])); /** @var File $file */ $file = Injector::inst()->create($fileClass); // check canCreate permissions if (!$file->canCreate(null, $data)) { $result = ['message' => ['type' => 'error', 'value' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.CreatePermissionDenied', 'You do not have permission to add files')]]; return (new HTTPResponse(json_encode($result), 403))->addHeader('Content-Type', 'application/json'); } $uploadResult = $upload->loadIntoFile($tmpFile, $file, $parentRecord ? $parentRecord->getFilename() : '/'); if (!$uploadResult) { $result = ['message' => ['type' => 'error', 'value' => _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.LoadIntoFileFailed', 'Failed to load file')]]; return (new HTTPResponse(json_encode($result), 400))->addHeader('Content-Type', 'application/json'); } $file->ParentID = $parentRecord ? $parentRecord->ID : 0; $file->write(); $result = [$this->getObjectFromData($file)]; return (new HTTPResponse(json_encode($result)))->addHeader('Content-Type', 'application/json'); }