public function testFileSetAndGet() { $fileCount = FileModel::getCount(); $this->assertEquals(0, $fileCount); $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'); $this->assertEquals(6495, strlen($contents)); $fileContent = new FileContent(); $fileContent->content = $contents; $file = new FileModel(); $file->fileContent = $fileContent; $file->name = 'testNote.txt'; $file->type = ZurmoFileHelper::getMimeType($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'); $file->size = filesize($filePath); $saved = $file->save(); $this->assertTrue($saved); $fileId = $file->id; $file->forget(); //Now retrieve the file and make sure the content matches. $file = FileModel::getById($fileId); $this->assertEquals($contents, $file->fileContent->content); $this->assertEquals('testNote.txt', $file->name); $this->assertEquals('text/plain', $file->type); $this->assertEquals(6495, $file->size); //Remove the fileModel. The related fileContent should also be removed because it is OWNED by the fileModel. $this->assertEquals(1, FileModel::getCount()); $this->assertEquals(1, FileContent::getCount()); $file->delete(); $this->assertEquals(0, FileModel::getCount()); $this->assertEquals(0, FileContent::getCount()); }
public static function resetAndPopulateFilesArrayByFilePathAndName($arrayName, $filePath, $fileName) { assert('is_string($arrayName) && $arrayName != ""'); // Not Coding Standard assert('is_string($filePath) && $filePath != ""'); // Not Coding Standard assert('is_string($fileName) && $fileName != ""'); // Not Coding Standard $_FILES = null; CUploadedFile::reset(); $_FILES = array($arrayName => array('name' => $fileName, 'type' => ZurmoFileHelper::getMimeType($filePath), 'tmp_name' => $filePath, 'error' => UPLOAD_ERR_OK, 'size' => filesize($filePath))); }
public static function createImageFileModel($fileName = 'testImage.png') { $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . $fileName; $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . $fileName); $fileContent = new FileContent(); $fileContent->content = $contents; $file = new ImageFileModel(); $file->fileContent = $fileContent; $file->name = $fileName; $file->type = ZurmoFileHelper::getMimeType($pathToFiles . DIRECTORY_SEPARATOR . $fileName); $file->size = filesize($filePath); $saved = $file->save(); assert('$saved'); // Not Coding Standard return $file; }
/** * Reads image or thumb if it exists on cache. * If it don't exists cache it from the ImageFileModel first * @param $fileName The filename of image * @param bool $shouldGetThumbnail True if we want the thumbnail of the image */ public static function readImageFromCache($fileName, $shouldGetThumbnail = false) { assert('is_string($fileName)'); assert('is_bool($shouldGetThumbnail)'); $imagePath = ImageFileModel::getImageCachePathByFileName($fileName, $shouldGetThumbnail); if (!file_exists($imagePath)) { $imageFileModel = ImageFileModel::getByFileName($fileName); $imageFileModel->createImageCache($shouldGetThumbnail); } $mime = ZurmoFileHelper::getMimeType($imagePath); $size = filesize($imagePath); $name = pathinfo($imagePath, PATHINFO_FILENAME); header('Content-Type: ' . $mime); header('Content-Length: ' . $size); header('Content-Name: ' . $name); readfile($imagePath); Yii::app()->end(0, false); }
/** * * @param string $filePath * @param string $fileName * @return $fileModel or false on failure */ public static function makeByFilePathAndName($filePath, $fileName) { assert('is_string($filePath) && $filePath !=""'); assert('is_string($fileName) && $fileName !=""'); $contents = file_get_contents($filePath); if ($contents === false) { return false; } $fileContent = new FileContent(); $fileContent->content = $contents; $file = new FileModel(); $file->fileContent = $fileContent; $file->name = $fileName; $file->type = ZurmoFileHelper::getMimeType($filePath); $file->size = filesize($filePath); if (!$file->save()) { return false; } return $file; }
protected function populateWithFiles($model, $numberOfFilesToAttach, $pathToFiles) { assert('$model instanceof EmailTemplate || $model instanceof Autoresponder || $model instanceof Campaign'); for ($i = 0; $i < $numberOfFilesToAttach; $i++) { $fileName = $this->files[array_rand($this->files)]; $filePath = $pathToFiles . DIRECTORY_SEPARATOR . $fileName; $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . $fileName); $fileContent = new FileContent(); $fileContent->content = $contents; $file = new FileModel(); $file->fileContent = $fileContent; $file->name = $fileName; $file->type = ZurmoFileHelper::getMimeType($pathToFiles . DIRECTORY_SEPARATOR . $fileName); $file->size = filesize($filePath); $saved = $file->save(); if (!$saved) { throw new FailedToSaveModelException(); } $model->files->add($file); } }
public function actionTrack() { try { Yii::app()->user->userModel = BaseActionControlUserConfigUtil::getUserToRunAs(); $response = EmailMessageActivityUtil::resolveQueryStringFromUrlAndCreateOrUpdateActivity(); if ($response['redirect']) { $this->redirect($response['url']); } elseif (isset($response['imagePath'])) { $mime = ZurmoFileHelper::getMimeType($response['imagePath']); $size = filesize($response['imagePath']); $name = pathinfo($response['imagePath'], PATHINFO_FILENAME); header('Content-Type: ' . $mime); header('Content-Length: ' . $size); header('Content-Name: ' . $name); readfile($response['imagePath']); Yii::app()->end(0, false); } } catch (NotFoundException $e) { } catch (NotSupportedException $e) { } catch (FailedToSaveModelException $e) { } catch (MissingASuperAdministratorException $e) { } // we do not catch all exceptions because we need Exit and Redirect Exception for unit tests }
public static function saveLogoFile($fileName, $filePath, $fileModelIdentifier) { if (ZurmoConfigurationUtil::getByModuleName('ZurmoModule', $fileModelIdentifier) !== null) { $fileModelId = ZurmoConfigurationUtil::getByModuleName('ZurmoModule', $fileModelIdentifier); $file = FileModel::getById($fileModelId); $contents = file_get_contents($filePath); $file->fileContent->content = $contents; $file->name = $fileName; $file->type = ZurmoFileHelper::getMimeType($filePath); $file->size = filesize($filePath); $file->save(); return $file->id; } else { $contents = file_get_contents($filePath); $fileContent = new FileContent(); $fileContent->content = $contents; $file = new FileModel(); $file->fileContent = $fileContent; $file->name = $fileName; $file->type = ZurmoFileHelper::getMimeType($filePath); $file->size = filesize($filePath); $file->save(); return $file->id; } }
/** * Create FileModel * @param array $attachment * @return FileModel */ protected function createEmailAttachment($attachment) { // Save attachments if ($attachment['filename'] != null) { $fileContent = new FileContent(); $fileContent->content = $attachment['attachment']; $file = new FileModel(); $file->fileContent = $fileContent; $file->name = $attachment['filename']; $file->type = ZurmoFileHelper::getMimeType($attachment['filename']); $file->size = strlen($attachment['attachment']); $saved = $file->save(); assert('$saved'); // Not Coding Standard return $file; } else { return false; } }
/** * Create FileModel * @param array $attachment * @param bool $base64encoded * @return FileModel */ public static function createEmailAttachment($attachment, $base64encoded = false) { // Save attachments if ($attachment['filename'] != null && static::isAttachmentExtensionAllowed($attachment['filename'])) { $fileContent = new FileContent(); if ($base64encoded) { $fileContent->content = base64_decode($attachment['attachment']); } else { $fileContent->content = $attachment['attachment']; } $file = new FileModel(); $file->fileContent = $fileContent; $file->name = $attachment['filename']; $file->type = ZurmoFileHelper::getMimeType($attachment['filename']); $file->size = strlen($fileContent->content); $saved = $file->save(); assert('$saved'); // Not Coding Standard return $file; } else { return false; } }