/** * Write a new file using a stream. * * @param string $path * @param resource $resource * @param Config $config Config object * * @return array|false false on failure file meta data on success */ public function writeStream($path, $resource, Config $config) { $path = $this->applyPathPrefix($path); $params = $config->get('params', null); $mime = $config->get('mime', 'application/octet-stream'); $checkCrc = $config->get('checkCrc', false); list($ret, $code) = $this->ufileSdk->put($path, $resource, ['Content-Type' => $mime]); }
public function testGet() { $config = new Config(); $this->assertFalse($config->has('setting')); $this->assertNull($config->get('setting')); $config->set('setting', 'value'); $this->assertEquals('value', $config->get('setting')); $fallback = new Config(['fallback_setting' => 'fallback_value']); $config->setFallback($fallback); $this->assertEquals('fallback_value', $config->get('fallback_setting')); }
/** * Returns an array of options from the config. * * @param Config $config * @return array */ protected function getOptionsFromConfig(Config $config) { $options = []; if ($config->has('visibility')) { $options['acl'] = $config->get('visibility') === AdapterInterface::VISIBILITY_PUBLIC ? 'publicRead' : 'private'; } if ($config->has('mimetype')) { $options['mimetype'] = $config->get('mimetype'); } // TODO: consider other metadata which we can set here return $options; }
/** * Write a new file. * * @param string $path * @param string $contents * @param Config $config Config object * * @return array|false false on failure file meta data on success */ public function write($path, $contents, Config $config) { if ($config->has('ttl') && !$config->has('expirationType')) { $config->set('expirationType', self::EXPIRE_IN_SECONDS); } $args = array_merge([$path, $contents], array_filter([$config->get('expirationType'), $config->get('ttl'), $config->get('setFlag')], function ($value) { return !is_null($value); })); if (!call_user_func_array([$this->client, 'set'], $args)) { return false; } return compact('path', 'contents'); }
/** * Write a new file. * * @param string $path * @param string $contents * @param Config $config Config object * * @return array|false false on failure file meta data on success */ public function write($path, $contents, Config $config) { $auth = $this->getAuth(); $token = $auth->uploadToken($this->bucket, $path); $params = $config->get('params', null); $mime = $config->get('mime', 'application/octet-stream'); $checkCrc = $config->get('checkCrc', false); $upload_manager = $this->getUploadManager(); list($ret, $error) = $upload_manager->put($token, $path, $contents, $params, $mime, $checkCrc); if ($error !== null) { $this->logQiniuError($error); return false; } else { return $ret; } }
/** * @inheritdoc */ public function write($path, $contents, Config $config) { $type = 'file'; $result = compact('contents', 'type', 'path'); if ($visibility = $config->get('visibility')) { $result['visibility'] = $visibility; } return $result; }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); $headers = []; if ($config && $config->has('headers')) { $headers = $config->get('headers'); } $response = $this->container->uploadObject($location, $contents, $headers); return $this->normalizeObject($response); }
/** * {@inheritdoc} */ public function createDir($dirname, Config $config) { $location = $this->applyPathPrefix($dirname); $umask = umask(0); $visibility = $config->get('visibility', 'public'); if (!is_dir($location) && !@mkdir($location, $this->permissionMap['dir'][$visibility], true)) { $return = false; } else { $return = ['path' => $dirname, 'type' => 'dir']; } umask($umask); return $return; }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); $this->ensureDirectory(dirname($location)); if (($size = file_put_contents($location, $contents)) === false) { return false; } $type = 'file'; $result = compact('contents', 'type', 'size', 'path'); if ($visibility = $config->get('visibility')) { $result['visibility'] = $visibility; $this->setVisibility($path, $visibility); } return $result; }
/** * {@inheritdoc} */ public function writeStream($path, $resource, Config $config) { $location = $this->applyPathPrefix($path); $this->ensureDirectory(dirname($location)); $stream = fopen($location, 'wb+'); if ($stream === false) { return false; } stream_copy_to_stream($resource, $stream); if (!fclose($stream)) { return false; } if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return compact('path', 'visibility'); }
/** * {@inheritdoc} */ public function writeStream($path, $resource, Config $config) { $location = $this->applyPathPrefix($path); $this->ensureDirectory(dirname($location)); if (!($stream = fopen($location, 'w'))) { return false; } while (!feof($resource)) { fwrite($stream, fread($resource, 1024), 1024); } if (!fclose($stream)) { return false; } if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return compact('path', 'visibility'); }
/** * Returns an array of options from the config. * * @param Config $config * @return array */ protected function getOptionsFromConfig(Config $config) { $options = []; if ($visibility = $config->get('visibility')) { $options['predefinedAcl'] = $this->getPredefinedAclForVisibility($visibility); } else { // if a file is created without an acl, it isn't accessible via the console // we therefore default to private $options['predefinedAcl'] = $this->getPredefinedAclForVisibility(AdapterInterface::VISIBILITY_PRIVATE); } return $options; }
private function doMirror($originDir, $targetDir, Flysystem\Config $config) { if ($config->get('delete', true) && $this->doHas($targetDir)) { $it = $this->getIterator($targetDir, \RecursiveIteratorIterator::CHILD_FIRST); foreach ($it as $handler) { /** @var HandlerInterface $handler */ $origin = str_replace($targetDir, $originDir, $handler->getPath()); if (!$this->doHas($origin)) { if ($handler->isDir()) { $this->doDeleteDir($handler->getPath()); } else { $this->doDelete($handler->getPath()); } } } } if ($this->doHas($originDir)) { $this->doCreateDir($targetDir, $config); } $it = $this->getIterator($originDir, \RecursiveIteratorIterator::SELF_FIRST); foreach ($it as $handler) { $target = str_replace($originDir, $targetDir, $handler->getPath()); if ($handler->isDir()) { $this->doCreateDir($target, $config); } else { $this->doCopy($handler->getPath(), $target, $config->get('override')); } } }
/** * {@inheritdoc} */ public function update($path, $contents, Config $config) { if (!$this->hasFile($path)) { return false; } $this->storage[$path]['contents'] = $contents; $this->storage[$path]['timestamp'] = time(); $this->storage[$path]['size'] = Util::contentSize($contents); $this->storage[$path]['mimetype'] = Util::guessMimeType($path, $contents); if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return $this->getMetadata($path); }
/** * Upload|Update item * * @param string $path * @param string|resource $contents * @param Config $config * * @return array|false item info array */ protected function upload($path, $contents, Config $config) { list($parentId, $fileName) = $this->splitPath($path); $mode = 'update'; $mime = $config->get('mimetype'); $srcFile = $this->getFileObject($path); $file = new Google_Service_Drive_DriveFile(); if (!$srcFile) { $mode = 'insert'; $file->setName($fileName); $file->setParents([$parentId]); } $isResource = false; if (is_resource($contents)) { $fstat = @fstat($contents); if (!empty($fstat['size'])) { $isResource = true; } if (!$isResource) { $contents = stream_get_contents($contents); } } if ($isResource) { // set chunk size (max: 100MB) $chunkSizeBytes = 100 * 1024 * 1024; $memory = $this->getIniBytes('memory_limit'); if ($memory) { $chunkSizeBytes = min([$chunkSizeBytes, intval($memory / 4 / 256) * 256]); } if ($fstat['size'] < $chunkSizeBytes) { $isResource = false; $contents = stream_get_contents($contents); } } if (!$mime) { $mime = Util::guessMimeType($fileName, $isResource ? '' : $contents); } $file->setMimeType($mime); if ($isResource) { $client = $this->service->getClient(); // Call the API with the media upload, defer so it doesn't immediately return. $client->setDefer(true); if ($mode === 'insert') { $request = $this->service->files->create($file, ['fields' => self::FETCHFIELDS_GET]); } else { $request = $this->service->files->update($srcFile->getId(), $file, ['fields' => self::FETCHFIELDS_GET]); } // Create a media file upload to represent our upload process. $media = new Google_Http_MediaFileUpload($client, $request, $mime, null, true, $chunkSizeBytes); $media->setFileSize($fstat['size']); // Upload the various chunks. $status will be false until the process is // complete. $status = false; $handle = $contents; while (!$status && !feof($handle)) { // read until you get $chunkSizeBytes from TESTFILE // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file // An example of a read buffered file is when reading from a URL $chunk = $this->readFileChunk($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } // The final value of $status will be the data from the API for the object // that has been uploaded. if ($status != false) { $obj = $status; } $client->setDefer(false); } else { $params = ['data' => $contents, 'uploadType' => 'media', 'fields' => self::FETCHFIELDS_GET]; if ($mode === 'insert') { $obj = $this->service->files->create($file, $params); } else { $obj = $this->service->files->update($srcFile->getId(), $file, $params); } } if ($obj instanceof Google_Service_Drive_DriveFile) { $this->cacheFileObjects[$obj->getId()] = $obj; if ($mode === 'insert') { $this->cacheFileObjects[$fileName] = $obj; } $result = $this->normaliseObject($obj, Util::dirname($path)); if ($visibility = $config->get('visibility')) { if ($this->setVisibility($path, $visibility)) { $result['visibility'] = $visibility; } } return $result; } return false; }
public function write($path, $contents, Config $config, $isPutFile = false) { $token = $this->getToken($path); $params = $config->get('params', null); $mime = $config->get('mime', 'application/octet-stream'); $checkCrc = $config->get('checkCrc', false); $uploadMgr = $this->getUploadManager(); if ($isPutFile) { list($ret, $error) = $uploadMgr->putFile($token, $path, $contents, $params, $mime, $checkCrc); } else { list($ret, $error) = $uploadMgr->put($token, $path, $contents, $params, $mime, $checkCrc); } return $this->returnDeal($ret, $error); }
/** * Get options from the config. * * @param Config $config * @return array */ protected function getOptionsFromConfig(Config $config) { $options = $this->options; foreach (static::$mappingOptions as $option => $ossOption) { if (!$config->has($option)) { continue; } $options[$ossOption] = $config->get($option); } return $options; }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); $this->client->request('PUT', $location, $contents); $result = compact('path', 'contents'); if ($config->get('visibility')) { throw new LogicException(__CLASS__ . ' does not support visibility settings.'); } return $result; }
/** * @return string */ protected function getTempFilename() { $pid = getmypid(); $tempDir = $this->config->get('temp_dir'); return $tempDir . DIRECTORY_SEPARATOR . uniqid("flysystempdo-{$pid}-", true); }
protected function kvPut($path, $contents, Config $config, $file = null) { $addOrSet = is_null($file) ? 'set' : 'add'; $file['contents'] = $contents; $file['timestamp'] = time(); $file['size'] = Util::contentSize($contents); if ($visibility = $config->get('visibility')) { $file['visibility'] = $visibility; } if ($this->client->{$addOrSet}($path, $file)) { return $file + compact('path'); } return false; }
/** * Update a file * * @param string $path * @param string $contents * @param mixed $config Config object or visibility setting * @return array|bool */ public function update($path, $contents, Config $config) { $path = $this->clean($path); if (!$this->has($path)) { throw new \Exception('File not found' . $path); } $postData = ["file" => '/' . $path, 'content' => $contents, '_method' => 'put']; $this->request('put_content', $path, $postData); $size = strlen($contents); $mimetype = $this->getMimetype($path); if ($visibility = $config->get('visibility')) { $result['visibility'] = $visibility; $this->setVisibility($path, $visibility); } return compact('path', 'size', 'contents', 'mimetype'); }
/** * {@inheritdoc} */ public function update($path, $contents, Config $config) { if (!$this->hasFile($path)) { return false; } $this->storage[$path]['contents'] = $contents; $this->storage[$path]['timestamp'] = time(); $this->storage[$path]['size'] = Util::contentSize($contents); if ($visibility = $config->get('visibility')) { $this->storage[$path]['visibility'] = $visibility; } return $this->storage[$path] + compact('path'); }
/** * Retrieve options from a Config instance. * * @param Config $config * * @return array */ protected function getOptionsFromConfig(Config $config) { $options = []; foreach (static::$metaOptions as $option) { if (!$config->has($option)) { continue; } $options[$option] = $config->get($option); } if ($mimetype = $config->get('mimetype')) { // For local reference $options['mimetype'] = $mimetype; // For external reference $options['ContentType'] = $mimetype; } return $options; }
/** * Retrieve options from a Config instance. * * @param Config $config * * @return CreateBlobOptions */ protected function getOptionsFromConfig(Config $config) { $options = new CreateBlobOptions(); foreach (static::$metaOptions as $option) { if (!$config->has($option)) { continue; } call_user_func([$options, "set{$option}"], $config->get($option)); } if ($mimetype = $config->get('mimetype')) { $options->setContentType($mimetype); } return $options; }
/** * Get options from the config. * * @param Config $config * * @return array */ protected function getOptionsFromConfig(Config $config) { $options = $this->options; if ($visibility = $config->get('visibility')) { // For local reference $options['visibility'] = $visibility; // For external reference $options['ACL'] = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? 'public-read' : 'private'; } if ($mimetype = $config->get('mimetype')) { // For local reference $options['mimetype'] = $mimetype; // For external reference $options['ContentType'] = $mimetype; } foreach (static::$metaOptions as $option) { if (!$config->has($option)) { continue; } $options[$option] = $config->get($option); } return $options; }
/** * {@inheritdoc} */ public function createDir($dirname, Config $config) { $info = $this->getPathInfo($dirname); $status = ['path' => $info['path'], 'type' => 'dir', 'visibility' => $config->get('visibility', 'public'), 'timestamp' => time()]; if (!$this->has($info['path'])) { if (!$this->ensurePathExists($info['dirname'], $config) || !($this->redis->hsetnx($this->applyPathPrefix($info['path']), '.', json_encode($status)) && $this->redis->hsetnx($this->applyPathPrefix($info['dirname']), $info['basename'], json_encode($status)))) { $status = false; } } elseif ($this->getMetadata($info['path'])['type'] === 'file') { $status = false; } return $status; }
/** * {@inheritdoc} */ public function createDir($dirname, Config $config) { $headers = $config->get('headers', []); $headers['Content-Type'] = 'application/directory'; $extendedConfig = (new Config())->setFallback($config); $extendedConfig->set('headers', $headers); return $this->write($dirname, '', $extendedConfig); }
/** * Write a new file using a stream. * * @param string $path * @param resource $resource * @param Config $config Config object * * @return array|false false on failure file meta data on success */ public function writeStream($path, $resource, Config $config) { $model = $this->findByLocation($path); if (null === $model) { $model = $this->model->create(['location' => $path]); } while (!feof($resource)) { $model->content .= fread($resource, 1024); } if ($visibility = $config->get('visibility')) { $model->visibility = $visibility === true; } try { $model->save(); } catch (\Exception $e) { return false; } return compact('path', 'visibility'); }
/** * @inheritdoc */ public function writeStream($path, $resource, Config $config) { $this->ensureDirectory(Util::dirname($path)); if (!ftp_fput($this->getConnection(), $path, $resource, $this->transferMode)) { return false; } if ($visibility = $config->get('visibility')) { $this->setVisibility($path, $visibility); } return compact('path', 'visibility'); }
public function writeStreamWithoutKey($resource, Config $config) { $size = Util::getStreamSize($resource); $token = $config->get('token'); return $this->streamUpload(null, $resource, $size, 'application/octet-stream', null, $token); }