/**
 * ydd-fast
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yiqi.oss.cloud;


import com.yiqi.common.exception.ServiceException;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 本地存储
 *
 * @author douya
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 16:22
 */
public class GoggleStorageService extends CloudStorageService {

    private String localUrlPrefix;  //地址前缀

    private String localPath; //储存根路径

    public GoggleStorageService(CloudStorageConfig config) {
        this.config = config;
        init();
    }

    private void init() {
        localUrlPrefix = config.getLocalUrlPrefix();
        localPath = config.getLocalPath();
    }

    @Override
    public String upload(byte[] data, String path) {
        String suffix = path.substring(path.lastIndexOf("."));
        return uploadSuffix(data, suffix);
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        String suffix = path.substring(path.lastIndexOf("."));
        return uploadSuffix(inputStream, suffix);
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
        // 上传文件流
        try {
            String path = getPath(null, suffix);  //生成后缀

            String bucketName = "fugou2";

            //读取本地存储的服务账号的json密钥，拿到该服务账号的权限
//            GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/home/auction/disco-aegis-349012-962e70d3c0fb.json"))
//                    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
//            //创建服务账号对应的操作对象
//            Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
//
//            // 将本地指定路径的文件转换为字节
//            byte[] bytes = IOUtils.toByteArray(inputStream);
//
//            //上传文件图片到指定的存储桶中
//            BlobId blobId = BlobId.of(bucketName, path);
//            BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
//            Blob blob = storage.create(blobInfo, bytes);
//
//            //修改已经上传的文件类型为 image/jpg
//            blob.toBuilder().setContentType("image/jpg").build().update();

            //返回公开访问的地址
            return "https://storage.googleapis.com/" + bucketName + "/" + path;

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("上传文件失败");
        }
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
        return uploadSuffix(new ByteArrayInputStream(data), suffix);
    }

}
