/** * @param Sport $sport * @param array $data */ protected function postSave(Sport $sport, array $data) { // upload picture if (isset($data['meta']) && isset($data['meta']['filename']) && !empty($data['meta']['filename'])) { $module = $this->getServiceContainer()->getModuleManager()->load('gossi/trixionary'); // ensure directory $dir = new Directory($module->getSportPath($sport)); if (!$dir->exists()) { $dir->make(); } $destpath = $module->getSkillPreviewPath($sport); $dest = new File($destpath); if ($dest->exists()) { $dest->delete(); } $file = new File($module->getUploadPath()->append($data['meta']['filename'])); $imagine = new Imagine(); $image = $imagine->open($file->toPath()->toString()); $image->save($dest->toPath()->toString()); $file->delete(); $sport->setSkillPictureUrl($module->getSkillPreviewUrl($sport)); $sport->save(); } // delete picture if (isset($data['meta']) && isset($data['meta']['skill_picture_delete']) && $data['meta']['skill_picture_delete'] == 1) { $module = $this->getServiceContainer()->getModuleManager()->load('gossi/trixionary'); $path = $module->getSkillPreviewPath($sport); $file = new File($path); $file->delete(); $sport->setSkillPictureUrl(null); $sport->save(); } }
/** * Writes contents to the file * * @param string $contents * @return $this */ public function write($contents) { $dir = new Directory($this->getDirname()); $dir->make(); file_put_contents($this->pathname, $contents); return $this; }
public function testIterator() { $dir = new Directory($this->root->url() . '/prj'); $dir->make(); $path = $dir->toPath(); $composer = $path->append('composer.json'); $file = $composer->toFileDescriptor()->toFile(); $file->write('{}'); $vendor = $path->append('vendor'); $folder = $vendor->toFileDescriptor()->toDirectory(); $folder->make(); $arr = new ArrayObject(); foreach ($dir as $k => $file) { if (!$file->isDot()) { $this->assertTrue($file instanceof FileDescriptor); $arr[$k] = $file->getFilename(); if ($file->isFile()) { $this->assertEquals('composer.json', $file->getFilename()); } if ($file->isDir()) { $this->assertEquals('vendor', $file->getFilename()); } } } $this->assertEquals(['composer.json', 'vendor'], $arr->sort()->toArray()); }
/** * @param Picture $picture * @param array $data */ protected function postSave(Picture $picture, array $data) { if (isset($data['meta']) && isset($data['meta']['filename'])) { $module = $this->getServiceContainer()->getModuleManager()->load('gossi/trixionary'); $file = new File($module->getUploadPath()->append($data['meta']['filename'])); $slugifier = new Slugify(); $filename = sprintf('%s-%u.%s', $slugifier->slugify($picture->getAthlete()), $picture->getId(), $file->getExtension()); $filepath = $module->getPicturesPath($picture->getSkill())->append($filename); $file->move($filepath); // create thumb folder $thumbspath = $module->getPicturesPath($picture->getSkill())->append('thumbs'); $dir = new Directory($thumbspath); if (!$dir->exists()) { $dir->make(); } // create thumb $imagine = new Imagine(); $image = $imagine->open($filepath->toString()); $max = max($image->getSize()->getWidth(), $image->getSize()->getHeight()); $width = $image->getSize()->getWidth() / $max * self::THUMB_MAX_SIZE; $height = $image->getSize()->getHeight() / $max * self::THUMB_MAX_SIZE; $size = new Box($width, $height); $thumbpath = $thumbspath->append($filename); $image->thumbnail($size)->save($thumbpath->toString()); // save to picture $picture->setUrl($module->getPicturesUrl($picture->getSkill()) . '/' . $filename); $picture->setThumbUrl($module->getPicturesUrl($picture->getSkill()) . '/thumbs/' . $filename); $picture->save(); } // activity $user = $this->getServiceContainer()->getAuthManager()->getUser(); $user->newActivity(array('verb' => $this->isNew ? Activity::VERB_UPLOAD : Activity::VERB_EDIT, 'object' => $picture, 'target' => $picture->getSkill())); }
/** * Creates a symlink to the given destination * * @param string|Path $destination */ public function linkTo($destination) { $target = new FileDescriptor($destination); $targetDir = new Directory($target->getDirname()); $targetDir->make(); $ok = false; if ($target->isLink()) { if (!$target->getLinkTarget()->equals($this->pathname)) { $target->delete(); } else { $ok = true; } } if (!$ok && @symlink($this->pathname, $target->getPathname()) !== true) { $report = error_get_last(); if (is_array($report) && DIRECTORY_SEPARATOR === '\\' && strpos($report['message'], 'error code(1314)') !== false) { throw new FileException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); } throw new FileException(sprintf('Failed to create symbolic link from %s to %s', $this->pathname, $targetDir)); } }
/** * @expectedException phootwork\file\exception\FileException */ public function testDeleteWithFailure() { $dir = new Directory($this->root->url() . '/dir'); $dir->make(); $file = new File($this->root->url() . '/dir/composer.json'); $file->touch(); $this->assertTrue($file->exists()); $dir->setMode(0555); $file->delete(); $this->assertFalse($file->exists()); }
/** * Returns the path for managed files for this module * * @return Path */ public function getFilesPath() { $repo = $this->getServiceContainer()->getResourceRepository(); if (!$repo->contains($this->getFilesPuliPath())) { $dir = new Directory($repo->get('/files')->getFilesystemPath()); $path = $dir->toPath()->append('managed/' . $this->model->getName()); $dir = new Directory($path); $dir->make(); } $dir = new Directory($repo->get($this->getFilesPuliPath())->getFilesystemPath()); return $dir->toPath(); }