package com.fdkankan.manage.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import com.fdkankan.common.constant.UploadFilePath;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.DateExtUtil;
import com.fdkankan.fyun.oss.UploadToOssUtil;
import com.fdkankan.manage.service.ICommonService;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author dengsixing
 * @since 2022/6/7
 **/
@Service
public class CommonServiceImpl implements ICommonService {

    @Autowired
    private UploadToOssUtil uploadToOssUtil;

    @Override
    public ResultData uploadFile(MultipartFile file) throws IOException {
        String uuid = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        String  extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String ossPath = String.format(UploadFilePath.MANAGE_FILE_PATH, DateUtil.format(Calendar.getInstance()
            .getTime(), DateExtUtil.dateStyle6), uuid + extName);
        File tempFile = File.createTempFile(uuid ,extName);
        file.transferTo(tempFile);
        uploadToOssUtil.upload(tempFile.getPath(), ossPath);
        tempFile.deleteOnExit();
        return ResultData.ok(ossPath);

    }
}
