<%_ if (position === 'import') { _%>
import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.UUID;
import java.io.File;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import <%= packageName %>.web.rest.errors.BadRequestAlertException;
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import cn.xuyanwu.spring.file.storage.FileStorageService;
<%_ } _%>
<%_ if (position === 'method') { _%>

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


<%_ } _%>
