<%_ if (position === 'import') { _%>
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import java.io.File;
import java.net.URL;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import <%= packageName %>.web.rest.errors.BadRequestAlertException;
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
<%_ } _%>
<%_ if (position === 'method') { _%>

     /**
     * Save a uploadImage.
     *
     * @param uploadImageDTO the entity to save
     * @return the persisted entity
     */
    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
    <%_ } _%>
    public UploadImageDTO save(UploadImageDTO uploadImageDTO) {
        log.debug("Request to save UploadImage : {}", uploadImageDTO);
        if (!uploadImageDTO.getImage().isEmpty()) {
            final String yearAndMonth = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM"));
            uploadImageDTO.setCreateAt(ZonedDateTime.now());
            uploadImageDTO.setFullName(uploadImageDTO.getImage().getOriginalFilename());
            uploadImageDTO.setName(uploadImageDTO.getImage().getName());
            uploadImageDTO.setFolder(yearAndMonth + File.separator);
            uploadImageDTO.setFileSize(uploadImageDTO.getImage().getSize());
            final String savePathNew = yearAndMonth + File.separator;
            FileInfo upload = fileStorageService.of(uploadImageDTO.getImage()).setPath(savePathNew).upload();
            uploadImageDTO.setPath(upload.getBasePath() + upload.getPath() + upload.getFilename());
            uploadImageDTO.setUrl(upload.getUrl());
            uploadImageDTO.setExt(upload.getExt());
        } else {
            throw new BadRequestAlertException("Invalid file", "UploadFile", "imagesnull");
        }
        UploadImage uploadImage = uploadImageMapper.toEntity(uploadImageDTO);
        <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
        this.saveOrUpdate(uploadImage);
        return uploadImageMapper.toDto(this.getById(uploadImage.getId()));
        <%_ } else { _%>
        uploadImage = uploadImageRepository.save(uploadImage);
        return uploadImageMapper.toDto(uploadImage);
        <%_ } _%>
    }

    @Transactional
    public UploadImageDTO save(File file) {
        log.debug("Request to save UploadImage : {}", file.getName());
        if (file.exists()) {
            UploadImageDTO uploadImageDTO = new UploadImageDTO();
            final String yearAndMonth = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM"));
            uploadImageDTO.setCreateAt(ZonedDateTime.now());
            uploadImageDTO.setFullName(file.getName());
            uploadImageDTO.setName(file.getName());
            uploadImageDTO.setFolder(yearAndMonth + File.separator);
            uploadImageDTO.setFileSize(file.length());
            FileInfo upload = fileStorageService.of(file).setPlatform("local").upload();
            uploadImageDTO.setUrl(upload.getUrl());
            uploadImageDTO.setExt(upload.getExt());
            UploadImage uploadImage = uploadImageMapper.toEntity(uploadImageDTO);
            <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
            this.saveOrUpdate(uploadImage);
            return uploadImageMapper.toDto(this.getById(uploadImage.getId()));
            <%_ } else { _%>
            uploadImage = uploadImageRepository.save(uploadImage);
            return uploadImageMapper.toDto(uploadImage);
            <%_ } _%>
        } else {
            throw new BadRequestAlertException("Invalid file", "UploadFile", "imagesnull");
        }
    }

    @Transactional
    public UploadImageDTO save(URL url) {
        log.debug("Request to save UploadImage : {}", url.getPath());
        UploadImageDTO uploadImageDTO = new UploadImageDTO();
        final String yearAndMonth = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM"));
        uploadImageDTO.setCreateAt(ZonedDateTime.now());
        uploadImageDTO.setFolder(yearAndMonth + File.separator);
        FileInfo upload = fileStorageService.of(url).setPlatform("local").upload();
        uploadImageDTO.setFullName(upload.getFilename());
        uploadImageDTO.setName(upload.getFilename());
        uploadImageDTO.setUrl(upload.getUrl());
        uploadImageDTO.setExt(upload.getExt());
        uploadImageDTO.setFileSize(upload.getSize());
        UploadImage uploadImage = uploadImageMapper.toEntity(uploadImageDTO);
        <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
        this.saveOrUpdate(uploadImage);
        return uploadImageMapper.toDto(this.getById(uploadImage.getId()));
        <%_ } else { _%>
        uploadImage = uploadImageRepository.save(uploadImage);
        return uploadImageMapper.toDto(uploadImage);
        <%_ } _%>
    }
<%_ } _%>
