| | |
| | | package com.lf.server.service.data; |
| | | |
| | | import com.lf.server.entity.data.MetaEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.service.all.BaseUploadService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @Autowired |
| | | MetaService metaService; |
| | | |
| | | @Autowired |
| | | MetaFileService metaFileService; |
| | | |
| | | /** |
| | | * 查询文件 |
| | | */ |
| | | public List<MetaFileEntity> selectFiles(String subPath) throws IOException { |
| | | String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; |
| | | |
| | | File file = new File(root); |
| | | if (!file.exists() && !file.isDirectory()) { |
| | | return null; |
| | | } |
| | | File[] files = file.listFiles(); |
| | | if (null == files || files.length == 0) { |
| | | return null; |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | for (File f : files) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | double sizes = FileHelper.sizeToMb(f.length()); |
| | | String filePath = subPath + File.separator + fileName; |
| | | |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setName(fileName); |
| | | mf.setSizes(sizes); |
| | | mf.setPath(filePath); |
| | | mf.setGuid(FileHelper.getFileMd5(f.getPath())); |
| | | |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 插入文件 |
| | | */ |
| | | public int insertFiles(MetaEntity entity, List<MetaFileEntity> list) { |
| | | public int insertFiles(MetaEntity entity, List<MetaEntity> list) { |
| | | int count = 0; |
| | | try { |
| | | String temp = pathHelper.getConfig().getTempPath(); |
| | | String root = pathHelper.getUploadFullPath(); |
| | | |
| | | for (MetaFileEntity mf : list) { |
| | | for (MetaEntity mf : list) { |
| | | String filePath = getFilePath(temp, root, mf); |
| | | |
| | | // 元数据 |
| | | MetaEntity me = createMetaEntity(entity, mf); |
| | | if (metaService.insert(me) < 1) { |
| | | continue; |
| | | } |
| | | |
| | | // 元数据文件 |
| | | MetaFileEntity mfe = createMetaFileEntity(me, mf, filePath); |
| | | if (metaFileService.insert(mfe) > 0) { |
| | | MetaEntity me = createMetaEntity(entity, mf, filePath); |
| | | if (metaService.insert(me) > 0) { |
| | | count++; |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 获取文件路径 |
| | | */ |
| | | private String getFilePath(String temp, String root, MetaFileEntity mf) { |
| | | private String getFilePath(String temp, String root, MetaEntity mf) { |
| | | // 移动文件 |
| | | File file = new File(temp + File.separator + mf.getPath()); |
| | | File newFile = new File(root + File.separator + mf.getGuid()); |
| | | |
| | | MetaFileEntity old = metaFileService.selectByGuid(mf.getGuid()); |
| | | MetaEntity old = metaService.selectByGuid(mf.getGuid()); |
| | | |
| | | String filePath = null; |
| | | if (null == old) { |
| | |
| | | /** |
| | | * 创建元数据实体 |
| | | */ |
| | | private MetaEntity createMetaEntity(MetaEntity entity, MetaFileEntity mf) { |
| | | private MetaEntity createMetaEntity(MetaEntity entity, MetaEntity mf, String filePath) { |
| | | MetaEntity me = new MetaEntity(); |
| | | me.setDepid(entity.getDepid()); |
| | | me.setEventid(StringHelper.getGuid()); |
| | | me.setDirid(entity.getDirid()); |
| | | me.setDepid(entity.getDepid()); |
| | | me.setVerid(entity.getVerid()); |
| | | me.setType("file"); |
| | | me.setGather(entity.getGather()); |
| | | me.setBatch(entity.getBatch()); |
| | | me.setDescr(entity.getDescr()); |
| | | me.setName(mf.getName()); |
| | | me.setType("file"); |
| | | me.setGuid(entity.getGuid()); |
| | | me.setPath(FileHelper.getRelativePath(filePath)); |
| | | me.setSizes(mf.getSizes()); |
| | | me.setCreateTime(entity.getCreateTime()); |
| | | me.setCreateUser(entity.getCreateUser()); |
| | | |
| | | return me; |
| | | } |
| | | |
| | | /** |
| | | * 创建元数据文件实体 |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(MetaEntity me, MetaFileEntity entity, String filePath) { |
| | | MetaFileEntity mfe = new MetaFileEntity(); |
| | | mfe.setName(entity.getName()); |
| | | mfe.setMetaid(me.getId()); |
| | | mfe.setGuid(entity.getGuid()); |
| | | mfe.setSizes(entity.getSizes()); |
| | | mfe.setPath(FileHelper.getRelativePath(filePath)); |
| | | mfe.setCreateUser(me.getCreateUser()); |
| | | mfe.setCreateTime(me.getCreateTime()); |
| | | |
| | | return mfe; |
| | | } |
| | | } |