public function uploadImagePreview(int $themeId, string $path) : string { $theme = $this->getThemeById($themeId); $dir = $theme->getId(); $name = sprintf('%s.png', GenerateRandomString::gen(self::GENERATE_FILENAME_LENGTH)); $newPath = sprintf('%s/%s', $dir, $name); if ($this->fileSystem->has($dir)) { $this->fileSystem->deleteDir($dir); } $this->fileSystem->write($newPath, file_get_contents($path)); $theme->setPreview($newPath); $this->themeRepository->saveTheme($theme); return $theme->getPreview(); }
/** * Uploads raw contents to the service. * * @param string $contents * @return array The meta of the file. */ public function uploadContents($name, $contents) { $this->filesystem->write($name, $contents); $meta = $this->filesystem->getMetadata($name); $urlGenerator = app('Flarum\\Forum\\UrlGenerator'); if (empty($this->settings->get('flagrow.image-upload.cdnUrl'))) { // if there is no cdnUrl $meta['url'] = $urlGenerator->toPath('assets/images/' . $name); } else { // if there is $meta['url'] = $this->settings->get('flagrow.image-upload.cdnUrl') . 'assets/images/' . $name; } return $meta; }
/** * Generate manipulated image. * @param string $path Image path. * @param array $params Image manipulation params. * @return string Cache path. * @throws FileNotFoundException * @throws FilesystemException */ public function makeImage($path, array $params) { $sourcePath = $this->getSourcePath($path); $cachedPath = $this->getCachePath($path, $params); if ($this->cacheFileExists($path, $params) === true) { return $cachedPath; } if ($this->sourceFileExists($path) === false) { throw new FileNotFoundException('Could not find the image `' . $sourcePath . '`.'); } $source = $this->source->read($sourcePath); if ($source === false) { throw new FilesystemException('Could not read the image `' . $sourcePath . '`.'); } // We need to write the image to the local disk before // doing any manipulations. This is because EXIF data // can only be read from an actual file. $tmp = tempnam(sys_get_temp_dir(), 'Glide'); if (file_put_contents($tmp, $source) === false) { throw new FilesystemException('Unable to write temp file for `' . $sourcePath . '`.'); } try { $write = $this->cache->write($cachedPath, $this->api->run($tmp, $this->getAllParams($params))); if ($write === false) { throw new FilesystemException('Could not write the image `' . $cachedPath . '`.'); } } catch (FileExistsException $exception) { // This edge case occurs when the target already exists // because it's currently be written to disk in another // request. It's best to just fail silently. } unlink($tmp); return $cachedPath; }
/** * @param ConfigInterface $config * * @return bool */ public function write(ConfigInterface $config) { $fileName = $config->getFileName() ?: Config::CONFIG_FILE_NAME; $array = $config->getCleanArray(); $contents = Yaml::dump($array); return $this->projectFileSystem->write($fileName, $contents); }
/** * Handles the command execution. * * @param UploadImage $command * @return null|string */ public function handle(UploadImage $command) { if ($command->postId) { // load the Post for this image $post = $this->posts->findOrFail($command->postId, $command->actor); } else { $post = null; } // todo check rights // todo validate file $image = new Image(); $image->user_id = $command->actor->id; $image->upload_method = 'local'; if ($post) { $image->post_id = $post->id; } $this->events->fire(new ImageWillBeSaved($post, $command->actor, $image, $command->file)); $file_name = sprintf('%d-%d-%s.jpg', $post ? $post->id : 0, $command->actor->id, str_random()); if (!$this->uploadDir->write($file_name, $command->file)) { // todo should throw error return null; } $appPath = parse_url($this->app->url(), PHP_URL_PATH); $image->file_name = sprintf('%s/assets/images/%s', $appPath, $file_name); $image->created_at = Carbon::now(); $image->save(); return $image; }
/** * Create file and return it's path or false on failed * * @param string $path parent dir path * @param string $name new file name * @return string|bool **/ protected function _mkfile($path, $name) { $path = $this->_joinPath($path, $name); if ($this->fs->write($path, '')) { return $path; } return false; }
/** * Write settings content of a namespace * * @param string $namespace * @param array $data * @return void */ protected function write($namespace, array $data) { $file = $this->adapter->getFileName($namespace); $contents = $this->adapter->prepareForWriting($data); if (!$this->fileSystem->has($file)) { $this->fileSystem->write($file, $contents); } $this->fileSystem->update($file, $contents); }
/** * Writes the given sitemap to the filesystem. The filename pattern is: * {MD5_Hash}.{Class_Name}.{Index}.xml * @param string $groupName * @param AbstractSitemap $sitemap * @return string The filename of the sitemap written */ protected function writeSitemap($groupName, AbstractSitemap $sitemap) { static $index = 0; $className = (new \ReflectionClass($sitemap))->getShortName(); $fileName = "{$groupName}.{$className}.{$index}.xml"; $this->filesystem->write($fileName, $sitemap->toString()); array_push($this->filesCreated, $fileName); $index++; return $fileName; }
/** * Create a new file. * * @param string $path * @param string $contents * @param string $visibility * @throws IoWriteException */ public function create($path, $contents = '', $visibility = self::VISIBILITY_DEFAULT) { try { $this->fs->write($path, $contents, $this->prepareConfig($visibility)); } catch (Error $ex) { throw new IoWriteException("File {$path} could not be created.", $ex); } catch (Exception $ex) { throw new IoWriteException("File {$path} could not be created.", $ex); } }
public function uploadAttachment(string $tmpFile, string $desiredFileName) : Attachment { $desiredFileName = FileNameFilter::filter($desiredFileName); $attachmentType = $this->factoryFileAttachmentType($tmpFile); if ($attachmentType instanceof FileAttachmentType) { $this->validateFileSize($tmpFile, $attachmentType); } $subDirectory = join('/', str_split(GenerateRandomString::gen(12), 2)); $storagePath = $subDirectory . '/' . $desiredFileName; $publicPath = sprintf('%s/%s/%s', $this->wwwDir, $subDirectory, $desiredFileName); $finfo = new \finfo(FILEINFO_MIME); $content = file_get_contents($tmpFile); $contentType = $finfo->buffer($content); if ($this->fileSystem->write($storagePath, $content) === false) { throw new \Exception('Failed to copy uploaded file'); } $result = new Result($publicPath, $content, $contentType); $source = new LocalSource($publicPath, $storagePath); return $this->linkAttachment($publicPath, $subDirectory, $desiredFileName, $result, $source); }
/** * @param FilesystemInterface $filesystem * @param string $fileName * * @throws StorageException */ public function __construct(FilesystemInterface $filesystem, $fileName = self::DEFAULT_FILENAME) { if (!$filesystem->has($fileName)) { $filesystem->write($fileName, ''); } $handler = $filesystem->get($fileName); if (!$handler->isFile()) { throw new StorageException(sprintf('Expected path "%s" to be a file but its a "%s".', $handler->getPath(), $handler->getType())); } $this->file = $handler; }
private function backupAndUpdate($path, $content) { if ($this->enableBackup) { $oldContent = $this->master->read($path); if ($oldContent !== false) { if ($this->backup->has($path)) { $this->backup->update($path, $oldContent); } else { $this->backup->write($path, $oldContent); } } } $this->master->update($path, $content); }
/** * Generate manipulated image. * @return Request The request object. * @throws NotFoundException */ public function makeImage() { $request = $this->resolveRequestObject(func_get_args()); if ($this->cacheFileExists($request) === true) { return $request; } if ($this->sourceFileExists($request) === false) { throw new NotFoundException('Could not find the image `' . $this->getSourcePath($request) . '`.'); } $source = $this->source->read($this->getSourcePath($request)); if ($source === false) { throw new FilesystemException('Could not read the image `' . $this->getSourcePath($request) . '`.'); } try { $write = $this->cache->write($this->getCachePath($request), $this->api->run($request, $source)); } catch (FileExistsException $exception) { // Cache file failed to write. Fail silently. return $request; } if ($write === false) { throw new FilesystemException('Could not write the image `' . $this->getCachePath($request) . '`.'); } return $request; }
/** * Write a new file. * * @param string $path The path of the new file. * @param string $contents The file contents. * @param array $config An optional configuration array. * * @throws FileExistsException * * @return bool True on success, false on failure. */ public function write($path, $contents, array $config = []) { return $this->fileSystem->write($path, $contents, $config); }
/** * @param $path * @param $content * @return mixed */ public function writeContent($path, $content) { $result = $this->filesystem->write($path, $content); return $result; }
/** * @param string $path * @param FilesystemInterface $remote * @param AdapterInterface $remoteAdapter */ private function mirrorFile($path, FilesystemInterface $remote, AdapterInterface $remoteAdapter) { $masterContent = $this->master->read($path); if (!is_string($masterContent)) { throw new \RuntimeException(sprintf('File %s could not be read on master storage', $path)); } $isOnRemote = $remote->has($path); if ($isOnRemote && !$remote->update($path, $masterContent)) { throw $this->createRuntimeException('File', $path, 'updated', $remoteAdapter); } elseif (!$isOnRemote && !$remote->write($path, $masterContent)) { throw $this->createRuntimeException('File', $path, 'created', $remoteAdapter); } }
/** * Create file and return it's path or false on failed * * @param string $path parent dir path * @param string $name new file name * @return string|bool **/ protected function _mkfile($path, $name) { $path = $this->_joinPath($path, $name); return $this->_resultPath($this->fs->write($path, ''), $path); }