/** * Upload an object. * * @param $path * @param $body * @param Config $config * * @return array */ protected function upload($path, $body, Config $config) { $key = $this->applyPathPrefix($path); $mimetype = MimeType::detectByFileExtension(pathinfo($path, PATHINFO_EXTENSION)); $config->set('mimetype', $mimetype); $return = parent::upload($path, $body, $config); if (function_exists('getimagesizefromstring') && strpos($mimetype, 'image') !== false) { if (is_resource($body)) { rewind($body); $size = getimagesizefromstring(stream_get_contents($body)); } else { $size = getimagesizefromstring($body); } $this->s3Client->copyObject(['Bucket' => $this->bucket, 'CopySource' => $this->bucket . DS . $key, 'ContentType' => $mimetype, 'Metadata' => ['width' => $size[0], 'height' => $size[1]], 'MetadataDirective' => 'REPLACE', 'Key' => $key]); } return $return; }
/** * Given an upload, move it onto the application filesystem and return * the file information. * * When manually setting the filename, be sure to include the unique prefix! * * @param Ushahidi\Core\Tool\UploadData $upload * @param String $filename file to overwrite or create * @return Ushahidi\Core\Tool\FileData */ public function upload(UploadData $file, $filename = null) { if (!$filename) { // Use the upload filename, adding a unique prefix to prevent collisions. $filename = uniqid() . '-' . $file->name; } // Avoid any possible issues with case sensitivity by forcing all files // to be made lowercase. $filename = strtolower($filename); // Add the first and second letters of filename to the directory path // to help segment the files, producing a more reasonable amount of // files per directory, eg: abc-myfile.png -> a/b/abc-myfile.png $filepath = implode('/', [$this->prefix, $filename[0], $filename[1], $filename]); // Remove any leading slashes on the filename, path is always relative. $filepath = ltrim($filepath, '/'); // Stream the temporary file into the filesystem, creating or overwriting. $stream = fopen($file->tmp_name, 'r+'); $extension = pathinfo($filepath, PATHINFO_EXTENSION); $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; $config = ['mimetype' => $mimeType]; $this->fs->putStream($filepath, $stream, $config); if (is_resource($stream)) { fclose($stream); } // Get meta information about the file. $size = $this->fs->getSize($filepath); $type = $this->fs->getMimetype($filepath); // Get width and height of file, if it is an image. if ($this->isImage($type)) { list($width, $height) = @getimagesize($file->tmp_name); } else { $width = $height = null; } // And return the new file information. return new FileData(['file' => $filepath, 'size' => $size, 'type' => $type, 'width' => $width, 'height' => $height]); }
/** * Returns the file MIME type. * * @param string $path * * @return array|false|null|string */ public function getMimeType($path) { $ext = pathinfo($path, PATHINFO_EXTENSION); $mimeType = MimeType::detectByFileExtension($ext); return $mimeType ? array('mimetype' => $mimeType) : parent::getMimetype($path); }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string|resource $content * * @return string|null MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (!(empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) { return $mimeType; } return MimeType::detectByFilename($path); }
/** * @param $object * @param $uploadFile * @param int $size * @return null * @throws \OSS\Core\OssException */ protected function streamUpload($object, $uploadFile, $size = null) { $client = $this->getClient(); $bucket = $this->bucket; $upload_position = 0; $upload_file_size = $size ?: Util::getStreamSize($uploadFile); $extension = pathinfo($object, PATHINFO_EXTENSION); $options = [OssClient::OSS_CONTENT_TYPE => MimeType::detectByFileExtension($extension) ?: OssClient::DEFAULT_CONTENT_TYPE, OssClient::OSS_PART_SIZE => OssClient::OSS_MID_PART_SIZE]; $is_check_md5 = false; $uploadId = $client->initiateMultipartUpload($bucket, $object, $options); // 获取的分片 $pieces = $client->generateMultiuploadParts($upload_file_size, (int) $options[OssClient::OSS_PART_SIZE]); $response_upload_part = array(); foreach ($pieces as $i => $piece) { $from_pos = $upload_position + (int) $piece[OssClient::OSS_SEEK_TO]; $to_pos = (int) $piece[OssClient::OSS_LENGTH] + $from_pos - 1; $up_options = array(OssClient::OSS_FILE_UPLOAD => $uploadFile, OssClient::OSS_PART_NUM => $i + 1, OssClient::OSS_SEEK_TO => $from_pos, OssClient::OSS_LENGTH => $to_pos - $from_pos + 1, OssClient::OSS_CHECK_MD5 => $is_check_md5); if ($is_check_md5) { $content_md5 = OssUtil::getMd5SumForFile($uploadFile, $from_pos, $to_pos); $up_options[OssClient::OSS_CONTENT_MD5] = $content_md5; } $response_upload_part[] = $client->uploadPart($bucket, $object, $uploadId, $up_options); } $uploadParts = array(); foreach ($response_upload_part as $i => $etag) { $uploadParts[] = array('PartNumber' => $i + 1, 'ETag' => $etag); } return $client->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts); }
/** * Creates or updates a file inside current working folder using stream * * @param string $fileName file name * @param resource $resource file data stream * * @return bool true if updated successfully */ public function putStream($fileName, $resource) { $backend = $this->getBackend(); $filePath = Path::combine($this->getPath(), $fileName); $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $mimeType = MimeType::detectByFileExtension($ext); $options = $mimeType ? array('mimetype' => $mimeType) : array(); return $backend->putStream($filePath, $resource, $options); }
/** * @inheritdoc */ public function getMimetype($path) { $location = $this->applyPathPrefix($path); $finfo = new Finfo(FILEINFO_MIME_TYPE); $mimetype = $finfo->file($location); if (in_array($mimetype, ['application/octet-stream', 'inode/x-empty'])) { $mimetype = Util\MimeType::detectByFilename($location); } return ['mimetype' => $mimetype]; }
/** * {@inheritdoc} */ public function getMimetype(string $path) { $path = self::normalizeDirectorySeparator($path); if (!$this->isFile($path) && !$this->has($path)) { throw new FileNotFoundException($path); } $explode = explode('.', $path); if ($extension = end($explode)) { $extension = strtolower($extension); } return MimeType::detectByFileExtension($extension); }
/** * 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); } }
/** * @param string $path * * @return null|string */ public final function guessMimeType($path) { //@NOTE: The github API does not return a MIME type, so we have to guess :-( if (strrpos($path, '.') > 1) { $extension = substr($path, strrpos($path, '.') + 1); $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } else { $content = $this->getFileContents($path); $mimeType = MimeType::detectByContent($content); } return $mimeType; }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string $content * * @return string|null MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm'])) { $extension = pathinfo($path, PATHINFO_EXTENSION); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }
/** * @param $baseDir * @param $file * @return array */ protected function handleFileMetaData($baseDir, $file = null) { $meta = ['type' => (string) $file['type'], 'path' => (string) $baseDir . '/' . (string) $file['name'], 'visibility' => 'public', 'timestamp' => (string) $file['mtime']]; $attributes = $file->attributes(); if ($attributes != null) { foreach ($attributes as $attr => $value) { $attr = (string) $attr; if (!isset($meta[$attr])) { $meta[(string) $attr] = (string) $value; } } } if (!isset($meta['mimetype']) && $meta['type'] != 'dir') { $meta['mimetype'] = \League\Flysystem\Util\MimeType::detectByFileExtension(pathinfo($file['name'], PATHINFO_EXTENSION)); } return $meta; }
/** * @param string $path * * @return null|string * * @throws \Github\Exception\ErrorException * @throws \Github\Exception\InvalidArgumentException * @throws \Github\Exception\RuntimeException */ public final function guessMimeType($path) { //@NOTE: The github API does not return a MIME type, so we have to guess :-( $meta = $this->getMetaData($path); if ($this->hasKey($meta, self::KEY_TYPE) && $meta[self::KEY_TYPE] === self::KEY_DIRECTORY) { $mimeType = self::MIME_TYPE_DIRECTORY; // or application/x-directory } else { $content = $this->getFileContents($path); $mimeType = MimeType::detectByContent($content); } return $mimeType; }
/** * Guess MIME Type based on the path of the file and it's content. * * @param string $path * @param string $content * * @return string|null MIME Type or NULL if no extension detected */ public static function guessMimeType($path, $content) { $mimeType = MimeType::detectByContent($content); if (empty($mimeType) || $mimeType === 'text/plain') { $extension = pathinfo($path, PATHINFO_EXTENSION); if ($extension) { $mimeType = MimeType::detectByFileExtension($extension) ?: 'text/plain'; } } return $mimeType; }
/** * @inheritdoc */ public function getMimetype($path) { $extension = pathinfo($path, PATHINFO_EXTENSION); $mimetype = MimeType::detectByFileExtension($extension) ?: 'text/plain'; return compact('path', 'mimetype'); }
/** * @inheritdoc */ public function getMimetype($path) { if (!($metadata = $this->getMetadata($path))) { return false; } $metadata['mimetype'] = MimeType::detectByFilename($path); return $metadata; }
/** * @inheritdoc */ public function listContents($directory = '', $recursive = false) { $options = ['marker' => 2000, 'max-keys' => 1000, 'bucket' => $this->bucket, 'prefix' => $directory]; $data = $this->client->getBucket($options); $result = []; foreach ($data->Contents as $v) { $result[] = ['type' => (int) $v->Size > 0 ? 'file' : 'dir', 'dirname' => Util::dirname($v->Key), 'path' => rtrim($v->Key, '/'), 'timestamp' => strtotime($v->LastModified), 'mimetype' => MimeType::detectByFileExtension(pathinfo($v->Key, PATHINFO_EXTENSION)), 'size' => (int) $v->Size]; } return Util::emulateDirectories($result); }