package com.ytpm.middle.service.impl;

import com.ytpm.attach.Attach;
import com.ytpm.attach.Image;
import com.ytpm.middle.service.AttachService;
import com.ytpm.middle.util.AliOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j(topic = "attach-service")
@Service
public class AttachServiceImpl implements AttachService {

    @Override
    public Attach uploadFile(MultipartFile multipartFile) {

        String originFilename = multipartFile.getOriginalFilename();
        Attach attach = new Attach();
        attach.setName(originFilename);
        try {
            attach.setUrl("https://file.ytmdm.com/"+AliOSSUtil.uploadAdvise(multipartFile.getInputStream(),originFilename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        attach.setFileSize(multipartFile.getSize());
        return attach;
    }

    @Override
    public Image uploadImage(MultipartFile multipartFile) {
        String imgName = multipartFile.getOriginalFilename();
        Image image = new Image();
        image.setOssName(imgName);
        try {
            image.setUrl("https://file.ytmdm.com/"+AliOSSUtil.uploadAdvise(multipartFile.getInputStream(), imgName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return image;
    }
}
