/** * {@inheritdoc} */ public function update($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); $mimetype = Util::guessMimeType($path, $contents); if (($size = file_put_contents($location, $contents)) === false) { return false; } return compact('path', 'size', 'contents', 'mimetype'); }
/** * 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) { list($ret, $err) = Qiniu_RS_Put($this->getClient(), $this->bucket, $path, $contents, null); if ($err !== null) { return false; } $mimetype = Util::guessMimeType($path, $contents); return compact('mimetype', 'path'); }
/** * Write a file * * @param string $path * @param string $contents * @param mixed $config * @return array file metadata */ public function write($path, $contents, $config = null) { $config = Util::ensureConfig($config); $options = $this->getOptions($path, array('Body' => $contents, 'ContentType' => Util::guessMimeType($path, $contents), 'ContentLength' => Util::contentSize($contents)), $config); $result = $this->client->putObject($options); if ($result === false) { return false; } return $this->normalizeObject($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 = null) { $tmpFile = tempnam(sys_get_temp_dir(), 'arconnect-static'); file_put_contents($tmpFile, $contents); $file = new \CURLFile($tmpFile, Util::guessMimeType($path, $tmpFile), basename($tmpFile)); $data = ['slug' => $path, 'file' => $file]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->apiUrl . '/' . $this->application); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); $fileInfo = curl_getinfo($ch); curl_close($ch); unlink($tmpFile); if ($fileInfo['http_code'] >= 200 && $fileInfo['http_code'] < 400) { $response = ['contents' => $contents, 'type' => $fileInfo['content_type'], 'size' => $fileInfo['size_download'], 'path' => $path]; return $response; } return false; }
/** * 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) { $object = $this->applyPathPrefix($path); $options = $this->getOptionsFromConfig($config); if (!isset($options[OssClient::OSS_LENGTH])) { $options[OssClient::OSS_LENGTH] = Util::contentSize($contents); } if (!isset($options[OssClient::OSS_CONTENT_TYPE])) { $options[OssClient::OSS_CONTENT_TYPE] = Util::guessMimeType($path, $contents); } try { $this->client->putObject($this->bucket, $object, $contents, $options); } catch (OssException $e) { return false; } $type = 'file'; $result = compact('type', 'path', 'contents'); $result['mimetype'] = $options[OssClient::OSS_CONTENT_TYPE]; $result['size'] = $options[OssClient::OSS_LENGTH]; return $result; }
/** * {@inheritdoc} */ public function getMetadata($path) { $info = $this->getPathInfo($path); if (!$this->has($path)) { throw new \League\Flysystem\FileNotFoundException("File not found at path: {$info['path']}"); } $metadata = json_decode($this->redis->hget($this->applyPathPrefix($info['dirname']), $info['basename']), TRUE); if ($metadata['type'] === 'file') { $metadata['contents'] = base64_decode($metadata['contents']); $metadata += ['size' => Util::contentSize($metadata['contents']), 'mimetype' => Util::guessMimeType($metadata['path'], $metadata['contents'])]; unset($metadata['contents']); } return $metadata; }
/** * Get the mime-type from a given path or content * * @param string $path * @param string|resource $content * @return string */ private function getMimeType($path, $content) { if (is_resource($content)) { $meta = stream_get_meta_data($content); $ext = pathinfo($meta['uri'], PATHINFO_EXTENSION); $map = MimeType::getExtensionToMimeTypeMap(); if (isset($map[$ext])) { return $map[$ext]; } else { return ''; } } else { return Util::guessMimeType($path, $content); } }
/** * @inheritdoc */ public function write($path, $contents, Config $config) { $stream = fopen('php://temp', 'w+b'); fwrite($stream, $contents); rewind($stream); $result = $this->writeStream($path, $stream, $config); fclose($stream); if ($result === false) { return false; } $result['contents'] = $contents; $result['mimetype'] = Util::guessMimeType($path, $contents); return $result; }
/** * @inheritdoc */ public function write($path, $contents, Config $config) { $result = $this->getClient()->putObject($this->bucket, $path, $contents); if ($result !== null) { return false; } $mimetype = Util::guessMimeType($path, $contents); return compact('mimetype', 'path'); }
/** * Write a file * * @param string $path * @param string $contents * @param mixed $config * @return array file metadata */ public function write($path, $contents, $config = null) { $mimeType = Util::guessMimeType($path, $contents); if (isset($config)) { if (isset($config["mimeType"])) { $mimeType = $config["mimeType"]; } } $config = Util::ensureConfig($config); $options = $this->getOptions($path, array('ContentType' => $mimeType, 'ContentLength' => Util::contentSize($contents)), $config); $dirname = dirname($path); $filename = basename($path); if ($this->has($path)) { $isUpdate = true; $file = $this->service->files->get($path); } else { $file = new Google_Service_Drive_DriveFile(); $file->setTitle($filename); $file->setMimeType($mimeType); if ($dirname != ".") { $parent = new Google_Service_Drive_ParentReference(); $parent->setId($dirname); $file->setParents(array($parent)); } } if (isset($isUpdate)) { $result = $this->service->files->update($file['id'], $file, array('data' => $contents, 'mimeType' => $mimeType, 'uploadType' => "multipart")); } else { $result = $this->service->files->insert($file, array('data' => $contents, 'mimeType' => $mimeType, 'uploadType' => "multipart")); } if ($result === false) { return false; } $result['mimeType'] = $mimeType; $result['title'] = $filename; $result['id'] = $result->getId(); return $this->normalizeObject($result); }
/** * Write a file * * @param string $path * @param string $contents * @param mixed $config * * @return array file metadata */ public function write($path, $contents, $config = null) { $options = $this->getOptions($path, array('Body' => $contents, 'ContentType' => Util::guessMimeType($path, $contents), 'ContentLength' => Util::contentSize($contents)), $config = Util::ensureConfig($config)); return $this->writeObject($options); }
/** * @inheritdoc */ public function write($path, $contents, Config $config) { $stream = fopen('php://temp', 'w+b'); fwrite($stream, $contents); rewind($stream); $result = $this->writeStream($path, $stream, $config); is_resource($stream) && fclose($stream); // TODO 去掉is_resource判断 $result['contents'] = $contents; $result['mimetype'] = Util::guessMimeType($path, $contents); return $result; }
public function getMimetype($path) { $mimetype = Util::guessMimeType($path, $this->read($path)['contents']); return ['mimetype' => $mimetype, 'path' => $path]; }
/** * Get the mimetype of a file. * * @param string $path * * @return array|false */ public function getMimetype($path) { $stream = tmpfile(); fwrite($stream, $this->client->get($path)); rewind($stream); return ['mimetype' => Util::guessMimeType(stream_get_meta_data($stream)['uri'], stream_get_contents($stream))]; }
/** * {@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); }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $options = $this->getOptions($path, ['Body' => $contents, 'ContentType' => Util::guessMimeType($path, $contents), 'ContentLength' => Util::contentSize($contents)], $config); return $this->writeObject($options); }
/** * @inheritdoc */ public function update($path, $contents, Config $config) { list(, $err) = $this->getUploadManager()->put($this->getUploadToken(), $path, $contents); if ($err !== null) { return false; } $mimetype = Util::guessMimeType($path, $contents); return compact('mimetype', 'path'); }
/** * Get the mimetype of a file. * * @param string $path * * @return array|false */ public function getMimetype($path) { $model = $this->findByLocation($path); if (null === $model) { return false; } $mimetype = Util::guessMimeType($model->location, $model->content); if (null === $mimetype) { return false; } return compact('mimetype'); }
/** * 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; }
/** * {@inheritdoc} */ public function writeStream($path, $resource, Config $config) { $options = $this->getOptions($path, ['Content' => $resource, 'ContentType' => Util::guessMimeType($path, $resource), 'ContentLength' => Util::getStreamSize($resource)], $config); if ($this->client->putObject($options) === false) { return false; } return true; }
/** * Retrieve the mime-type of an object * * @param string $path * @return null|string mime-type or null on failure */ public function getMimetype($path) { if (isset($this->cache[$path]['mimetype'])) { return $this->cache[$path]['mimetype']; } if (!($contents = $this->read($path))) { return false; } $mimetype = Util::guessMimeType($path, $contents); $this->cache[$path]['mimetype'] = $mimetype; return $mimetype; }
/** * Uploads a file to the Google Cloud Storage service. * * @param string $path * @param string $contents * @param Config $config * @return array */ protected function upload($path, $contents, Config $config) { $options = $this->getOptionsFromConfig($config); if (!isset($options['mimetype'])) { $options['mimetype'] = Util::guessMimeType($path, $contents); } if (!isset($options['acl'])) { $options['acl'] = 'private'; } $object = new \Google_Service_Storage_StorageObject(); $object->setName($path); $object->setContentType($options['mimetype']); $params = ['data' => $contents, 'uploadType' => 'media', 'mimeType' => $options['mimetype'], 'predefinedAcl' => $options['acl']]; $object = $this->service->objects->insert($this->bucket, $object, $params); return $this->normaliseObject($object); }
/** * Upload an object. * * @param $path * @param $body * @param Config $config * * @return array */ protected function upload($path, $body, Config $config) { $key = $this->applyPathPrefix($path); $options = $this->getOptionsFromConfig($config); $acl = isset($options['ACL']) ? $options['ACL'] : 'private'; if (!isset($options['ContentType']) && is_string($body)) { $options['ContentType'] = Util::guessMimeType($path, $body); } if (!isset($options['ContentLength'])) { $options['ContentLength'] = is_string($body) ? Util::contentSize($body) : Util::getStreamSize($body); } $this->s3Client->upload($this->bucket, $key, $body, $acl, ['params' => $options]); return $this->normalizeResponse($options, $key); }
/** * @param string $path * @param string $filename * @param string $contents * @param resource $resource * @param Config $config * @return array|false */ protected function doUpdate($path, $filename, $contents, $resource, Config $config) { $data = $this->findPathData($path); if (!is_array($data) || $data['type'] != 'file') { return false; } $data['size'] = filesize($filename); $data['mimetype'] = Util::guessMimeType($data['path'], $contents); $sql = "UPDATE {$this->pathTable} SET size = :size, mimetype = :mimetype WHERE path_id = :path_id"; $update = ['size' => $data['size'], 'mimetype' => $data['mimetype'], 'path_id' => $data['path_id']]; $stmt = $this->db->prepare($sql); if (!$stmt->execute($update)) { return false; } $this->deleteChunks($data['path_id']); $this->insertChunks($data['path_id'], $resource, (bool) $data['is_compressed']); $this->cleanupTemp($resource, $filename); $data['update_ts'] = date('Y-m-d H:i:s'); return $this->normalizeMetadata($data); }
/** * @inheritdoc */ public function getMimetype($path) { if (!($metadata = $this->read($path))) { return false; } $metadata['mimetype'] = Util::guessMimeType($path, $metadata['contents']); return $metadata; }
/** * Write a file * * @param string $path * @param string $contents * @param mixed $config * * @return array file metadata */ public function write($path, $contents, $config = null) { $options = $this->getOptions($path, array('Body' => $contents, 'ContentType' => Util::guessMimeType($path, $contents), 'ContentLength' => Util::contentSize($contents)), Util::ensureConfig($config)); $multipartLimit = $this->mbToBytes($options['Multipart']); if ($options['ContentLength'] > $multipartLimit) { $result = $this->putObjectMultipart($options); } else { $result = $this->client->putObject($options); } if ($result === false) { return false; } return $this->normalizeObject($options); }