/** * Imports a local file into the system. The file must be added to this path * somehow. That's what happens in tools/files/importers/. * If a $fr (FileRecord) object is passed, we assign the newly imported FileVersion * object to that File. If not, we make a new filerecord. * * @param string $pointer path to file * @param string|bool $filename * @param ConcreteFile|bool $fr * * @return number Error Code | \Concrete\Core\File\Version */ public function import($pointer, $filename = false, $fr = false) { if ($filename == false) { // determine filename from $pointer $filename = basename($pointer); } $fh = Loader::helper('validation/file'); $fi = Loader::helper('file'); $cf = Core::make('helper/concrete/file'); $sanitizedFilename = $fi->sanitize($filename); // test if file is valid, else return FileImporter::E_FILE_INVALID if (!$fh->file($pointer)) { return Importer::E_FILE_INVALID; } if (!$fh->extension($filename)) { return Importer::E_FILE_INVALID_EXTENSION; } if ($fr instanceof File) { $fsl = $fr->getFileStorageLocationObject(); } else { $fsl = StorageLocation::getDefault(); } if (!$fsl instanceof StorageLocation) { return Importer::E_FILE_INVALID_STORAGE_LOCATION; } // store the file in the file storage location. $filesystem = $fsl->getFileSystemObject(); $prefix = $this->generatePrefix(); try { $src = fopen($pointer, 'rb'); $filesystem->writeStream($cf->prefix($prefix, $sanitizedFilename), $src, array('visibility' => AdapterInterface::VISIBILITY_PUBLIC, 'mimetype' => Core::make('helper/mime')->mimeFromExtension($fi->getExtension($sanitizedFilename)))); } catch (\Exception $e) { return self::E_FILE_UNABLE_TO_STORE; } if (!$fr instanceof File) { // we have to create a new file object for this file version $fv = ConcreteFile::add($sanitizedFilename, $prefix, array('fvTitle' => $filename), $fsl); $fv->refreshAttributes($this->rescanThumbnailsOnImport); } else { // We get a new version to modify $fv = $fr->getVersionToModify(true); $fv->updateFile($sanitizedFilename, $prefix); $fv->refreshAttributes($this->rescanThumbnailsOnImport); } return $fv; }
/** * {@inheritDoc} */ public function getVersionToModify($forceCreateNew = false) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getVersionToModify', array($forceCreateNew)); return parent::getVersionToModify($forceCreateNew); }
/** * Imports a file in the default file storage location's incoming directory. * * @param string $filename * @param ConcreteFile|bool $fr * * @return number Error Code | \Concrete\Core\File\Version */ public function importIncomingFile($filename, $fr = false) { $fh = Loader::helper('validation/file'); $fi = Loader::helper('file'); $cf = Core::make('helper/concrete/file'); $sanitizedFilename = $fi->sanitize($filename); $default = StorageLocation::getDefault(); $storage = $default->getFileSystemObject(); if (!$storage->has(REL_DIR_FILES_INCOMING . '/' . $filename)) { return Importer::E_FILE_INVALID; } if (!$fh->extension($filename)) { return Importer::E_FILE_INVALID_EXTENSION; } // first we import the file into the storage location that is the same. $prefix = $this->generatePrefix(); try { $copied = $storage->copy(REL_DIR_FILES_INCOMING . '/' . $filename, $cf->prefix($prefix, $sanitizedFilename)); } catch (\Exception $e) { $copied = false; } if (!$copied) { $storage->write($cf->prefix($prefix, $sanitizedFilename), $storage->read(REL_DIR_FILES_INCOMING . '/' . $filename)); } if (!$fr instanceof File) { // we have to create a new file object for this file version $fv = ConcreteFile::add($sanitizedFilename, $prefix, array('fvTitle' => $filename), $default); $fv->refreshAttributes($this->rescanThumbnailsOnImport); foreach ($this->importProcessors as $processor) { if ($processor->shouldProcess($fv)) { $processor->process($fv); } } } else { // We get a new version to modify $fv = $fr->getVersionToModify(true); $fv->updateFile($sanitizedFilename, $prefix); $fv->refreshAttributes($this->rescanThumbnailsOnImport); } return $fv; }
/** * Returns a file version object that is to be written to. Computes whether we can use the current most recent version, OR a new one should be created */ public function getVersionToModify($forceCreateNew = false) { return parent::getVersionToModify($forceCreateNew); }