/*
目录更新
@param  string  $bucketName bucket名称
@param  string  $path      文件夹路径,SDK会补齐末尾的 '/'
@param  string  $bizAttr   目录属性
438243
public static function updateFolder($bucketName, $path, $bizAttr = null) {
        $path = self::normalizerPath($path, True);

        return self::updateBase($bucketName, $path, $bizAttr);
    }
1271400
public function createBucket($bucket_name)
    {
        if ($this->doesBucketExist($bucket_name)) {
            $this->addMessage("Bucket $bucket_name already exists");
            return true;
        }

        if (mkdir('s3://' . $bucket_name)) {
            return true;
        } else {
            $this->addMessage('Could not create bucket ' . $bucket_name);
            return false;
        }
    }
346107
public function copy($path, $newPath)
    {
        $result = $this->client->copyObject($this->bucket, $path, $this->bucket, $newPath);
        if (is_null($result)) {
            return false;
        }

        return true;
    }
1323140
public function rename($path, $newpath)
    {
        return (bool)$this->normalizeResponse(
            Cosapi::move($this->getBucket(), $path, $newpath, 1)
        );
    }
305535
public T addFile(final String name, final List<String> path, final byte[] newHash, final boolean isDirectory) {
        return addFile(name, path, newHash, isDirectory, null);
    }
124607
private Set<String> getBucketsForPath(String path) {

        Set<String> result = Sets.newHashSet();
        boolean foundBucket = false;
        for (int i = 0; i < m_bucketNames.size(); i++) {
            for (String bucketPath : m_bucketPathLists.get(i)) {
                if (CmsStringUtil.isPrefixPath(bucketPath, path)) {
                    String bucketName = m_bucketNames.get(i);
                    result.add(bucketName);
                    if (!BUCKET_OTHER.equals(bucketName)) {
                        foundBucket = true;
                    }
                }
            }
        }
        if (!foundBucket) {
            result.add(BUCKET_OTHER);
        }
        return result;
    }
179737
public Blob updateBlob(String bucketName, String blobName) {
    // [START updateBlob]
    Map<String, String> newMetadata = new HashMap<>();
    newMetadata.put("key", "value");
    storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(null).build());
    Blob blob =
        storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(newMetadata).build());
    // [END updateBlob]
    return blob;
  }
801103
public function copy($path, $newpath)
    {
        $command = $this->s3Client->getCommand(
            'copyObject',
            [
                'Bucket'     => $this->bucket,
                'Key'        => $this->applyPathPrefix($newpath),
                'CopySource' => urlencode($this->bucket . '/' . $this->applyPathPrefix($path)),
                'ACL'        => $this->getRawVisibility($path) === AdapterInterface::VISIBILITY_PUBLIC
                    ? 'public-read' : 'private',
            ] + $this->options
        );

        try {
            $this->s3Client->execute($command);
        } catch (S3Exception $e) {
            return false;
        }

        return true;
    }
1071705
private static function uploadFile($bucket, $srcPath, $dstPath, $bizAttr = null, $insertOnly = null) {
        $srcPath = realpath($srcPath);
	    $dstPath = self::cosUrlEncode($dstPath);

	    if (filesize($srcPath) >= self::MAX_UNSLICE_FILE_SIZE ) {
		    return array(
                'code' => COSAPI_PARAMS_ERROR,
                'message' => 'file '.$srcPath.' larger then 20M, please use uploadBySlicing interface',
                'data' => array()
            );
	    }

        $expired = time() + self::EXPIRED_SECONDS;
        $url = self::generateResUrl($bucket, $dstPath);
        $signature = Auth::createReusableSignature($expired, $bucket);
        $fileSha = hash_file('sha1', $srcPath);

        $data = array(
            'op' => 'upload',
            'sha' => $fileSha,
            'biz_attr' => (isset($bizAttr) ? $bizAttr : ''),
        );

        $data['filecontent'] = file_get_contents($srcPath);

        if (isset($insertOnly) && strlen($insertOnly) > 0) {
            $data['insertOnly'] = (($insertOnly == 0 || $insertOnly == '0' ) ? 0 : 1);
        }

        $req = array(
            'url' => $url,
            'method' => 'post',
            'timeout' => self::$timeout,
            'data' => $data,
            'header' => array(
                'Authorization: ' . $signature,
            ),
        );

        return self::sendRequest($req);
    }
473219
public function rename($path)
    {
        // This implementation supersedes the Flysystem's one. Indeed, while using connectors
        // such as the Amazon S3 (v3) connector, rename on directories does not work. A custom
        // implementation is then needed.
        $contents = $this->getFileSystem()->listContents($this->getPrefix(), true);

        // Filter files only.
        $filePaths = [];
        foreach ($contents as $content) {
            if ($content['type'] === 'file') {
                $filePaths[]= [
                    'source' => $content['path'],
                    'destination' => str_replace($this->getPrefix(), $path, $content['path'])];
            }
        }

        foreach ($filePaths as $renaming) {
            try {
                if ($this->getFileSystem()->rename($renaming['source'], $renaming['destination']) === false) {
                    throw new \common_exception_FileSystemError("Unable to rename '" . $this->getPrefix() . "' into '${path}'.");
                }
            } catch (FileExistsException $e) {
                throw new \common_exception_FileSystemError("Unable to rename '" . $this->getPrefix() . "' into '${path}'. File already exists.");
            }
        }

        if (!$this->deleteSelf()) {
            throw new \common_exception_FileSystemError("Could not finalize renaming of '" . $this->getPrefix() . "' into '${path}'.");
        }

        return true;
    }
20055
public static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
        File folder = new File(srcFolder);
        if (folder.list().length == 0) {
            addFileToZip(path, srcFolder, zip, true);
        } else {
            for (String fileName : folder.list()) {
                if (path.equals("")) {
                    addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
                } else {
                    addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
                }
            }
        }
    }
366209
public static function rename_dir($path, $new_path, $source_area = null, $target_area = null)
	{
		return static::rename($path, $new_path, $source_area, $target_area);
	}
717562
private function addFileToZip($path, $zip) {
        $handler = opendir(public_path($path));
        while (($filename = readdir($handler)) !== false) {
            if (!in_array($filename,array('.','..','.DS_Store'))) {
                if (is_dir($path . "/" . $filename)) {
                    $this->addFileToZip($path . "/" . $filename, $zip);
                } else { //将文件加入zip对象
                    $fullname = public_path($path) . "/" . $filename;
                    $localname = str_replace('vendor/laravel-ddoc/','','/'.$path.'/'.$filename);
                    $zip->addFile($fullname,$localname);
                }
            }
        }
        @closedir($path);
    }
824882
public function addOrUpdateAlias($alias, $path, array $pathParameters = array(), $locale = null, $flush = true, $title = null, $keywords = null, $description = null)
    {
        $seo = $this->createNewAlias($alias, $path, $pathParameters, $locale, $title, $keywords, $description);
        $allReady = $this->findMetaTag($seo->getKeyword());
        if ($allReady) {
            $allReady->setAlias($alias);
            $allReady->setTitle($title);
            $allReady->setKeywords($keywords);
            $allReady->setDescription($description);
            $this->em->persist($allReady);
        } else {
            if ($alias == null) {
                // no alias to add
                return;
            }
            $this->em->persist($seo);
        }
        if ($flush) {
            $this->em->flush();
        }
    }
558874
public function avg_bucket(string $name, string $bucketsPath = null): AvgBucket
    {
        return new AvgBucket($name, $bucketsPath);
    }
250871
public function create($name, $chunksPath, $storagePath, $token = null, $mimeType = null)
    {
        return new ChunkFile($name, $chunksPath, $storagePath, $token, $mimeType, $this->files);
    }
16779
public function add($path, $middleware = null)
    {
        if (!isset($middleware)) {
            $middleware = $path;
            $path = null;
        }

        if (!empty($path) && !ctype_digit($path) && $path[0] !== '/') {
            trigger_error("Middleware path '$path' doesn't start with a '/'", E_USER_NOTICE);
        }
        
        if (!is_callable($middleware)) {
            throw new \InvalidArgumentException("Middleware should be callable");
        }

        if (!empty($path)) {
            $middleware = $this->wrapMiddleware($path, $middleware);
        }
        
        $this->middlewares[] = $middleware;
        
        return $this;
    }