package com.bonus.obs.service.impl;

import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.obs.service.ObsService;
import com.bonus.obs.utils.ObsUtils;
import com.obs.services.model.DeleteObjectResult;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@Service
public class ObsServiceImpl implements ObsService {
    @Resource
    private ObsUtils obsUtils;

    /**
     * 文件上传
     *
     * @param file 文件流
     * @return 文件信息
     */
    @Override
    public R<PutObjectResult> uploadFile(MultipartFile file) {
        try {
            String objectKey = file.getOriginalFilename();
            objectKey = FileUtils.generateObjectName(objectKey);
            return obsUtils.uploadFile(objectKey, FileUtils.multipartFileToFile(file));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除文件从OBS
     *
     * @param objectKey 文件在OBS中的键
     */
    @Override
    public R<DeleteObjectResult> deleteFile(String objectKey) {
        return obsUtils.deleteFile(objectKey);
    }

    /**
     * 下载文件从OBS
     *
     * @param objectKey 文件在OBS中的键
     */
    @Override
    public R<ObsObject> downloadFile(String objectKey) {
        return obsUtils.downloadFile(objectKey);
    }

    /**
     * 文件上传
     *
     * @param files 文件流
     * @return 文件信息
     */
    @Override
    public R<PutObjectResult> uploadFiles(MultipartFile[] files) {
        return null;
    }
}
