| | |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.*; |
| | | import com.lf.server.service.data.DirService; |
| | | import com.lf.server.service.sys.TokenService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | |
| | | */ |
| | | @Service |
| | | public class BaseUploadService { |
| | | @Autowired |
| | | @Resource |
| | | protected DirService dirService; |
| | | |
| | | @Autowired |
| | | @Resource |
| | | protected PathHelper pathHelper; |
| | | |
| | | @Resource |
| | | protected TokenService tokenService; |
| | | |
| | | public final Log log = LogFactory.getLog(getClass()); |
| | | |
| | |
| | | } |
| | | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId()); |
| | | for (MetaFileEntity meta : metas) { |
| | | meta.setEventid(StringHelper.getGuid()); |
| | | meta.setDircode(dir.getCode()); |
| | | meta.setDepcode(ue.getDepcode()); |
| | | meta.setVerid(ver.getId()); |
| | | meta.setCreateUser(ue.getId()); |
| | | meta.setEpsgCode(epsgCode); |
| | | |
| | | if (StaticData.ZIP.equals(meta.getExtName())) { |
| | | List<MetaFileEntity> subs = getMapperFiles(path, dir, dirs, meta); |
| | | List<MetaFileEntity> subs = getMapperFiles(path, meta); |
| | | if (null != subs && subs.size() > 0) { |
| | | list.addAll(subs); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | list.add(meta); |
| | | } |
| | | setMetaType(list); |
| | | setDircode(list, dir); |
| | | |
| | | return list; |
| | | } |
| | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(String path, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta) { |
| | | private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) { |
| | | String tempPath = pathHelper.getConfig().getTempPath(); |
| | | String zipFile = tempPath + File.separator + meta.getPath(); |
| | | File file = new File(zipFile); |
| | |
| | | List<File> files = new ArrayList<>(); |
| | | getFilesByPath(files, subPath); |
| | | |
| | | return getMapperFiles(files, dir, dirs, meta, tempPath.length() + 1); |
| | | return getMapperFiles(files, meta, tempPath.length() + 1); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 获取映射文件 |
| | | */ |
| | | private List<MetaFileEntity> getMapperFiles(List<File> files, DirEntity dir, List<DirEntity> dirs, MetaFileEntity meta, int start) { |
| | | private List<MetaFileEntity> getMapperFiles(List<File> files, MetaFileEntity meta, int start) { |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (File f : files) { |
| | | boolean isGdb = isGdbFile(f); |
| | |
| | | continue; |
| | | } |
| | | |
| | | String dircode = getDirByPath(f.getPath(), fileName, dir, dirs); |
| | | if (isGdb) { |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, dircode, start); |
| | | List<MetaFileEntity> rs = getGdbMappers(f, meta, start); |
| | | if (null != rs && rs.size() > 0) { |
| | | list.addAll(rs); |
| | | } |
| | | continue; |
| | | } |
| | | if (isOsgb) { |
| | | list.add(getOsgbMapper(f, meta, dircode, start)); |
| | | list.add(getOsgbMapper(f, meta, start)); |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(f, meta, fileName, extName); |
| | | mf.setPath(f.getPath().substring(start)); |
| | | mf.setDircode(dircode); |
| | | list.add(mf); |
| | | } |
| | | |
| | |
| | | */ |
| | | private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) { |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setDircode(meta.getDircode()); |
| | | mf.setDepcode(meta.getDepcode()); |
| | | mf.setVerid(meta.getVerid()); |
| | | mf.setCreateUser(meta.getCreateUser()); |
| | | mf.setEpsgCode(meta.getEpsgCode()); |
| | | |
| | | return mf; |
| | | } |
| | | |
| | | /** |
| | | * 根据文件路径获取目录ID |
| | | */ |
| | | private String getDirByPath(String filePath,String fileName, DirEntity dir, List<DirEntity> dirs) { |
| | | if (0 != dir.getPid() || null == dirs || dirs.isEmpty()) { |
| | | return dir.getCode(); |
| | | } |
| | | if (StaticData.SLASH.equals(File.separator)) { |
| | | filePath = filePath.replace("/", "\\"); |
| | | } |
| | | |
| | | fileName = fileName.toLowerCase(); |
| | | filePath = filePath.toLowerCase(); |
| | | for (DirEntity entity : dirs) { |
| | | if (filePath.contains(entity.getFullName().toLowerCase() + "\\" + fileName)) { |
| | | return entity.getCode(); |
| | | } |
| | | } |
| | | |
| | | return dir.getCode(); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 获取GDB文件映射 |
| | | */ |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, String dircode, int start) { |
| | | private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int start) { |
| | | List<String> tabs = GdbHelper.getTabNames(f.getPath()); |
| | | if (null == tabs || tabs.size() == 0) { |
| | | return null; |
| | |
| | | List<MetaFileEntity> list = new ArrayList<>(); |
| | | for (String tab : tabs) { |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDircode(dircode); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName + "\\" + tab); |
| | | mf.setExtName(extName); |
| | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取OSGB文件映射 |
| | | */ |
| | | private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, String dircode, int start) { |
| | | private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, int start) { |
| | | String fileName = FileHelper.getFileName(f.getPath()); |
| | | |
| | | List<String> files = new ArrayList<>(); |
| | |
| | | double sizes = getFilesSize(files); |
| | | |
| | | MetaFileEntity mf = createMetaFileEntity(meta); |
| | | mf.setDircode(dircode); |
| | | mf.setEventid(StringHelper.getGuid()); |
| | | mf.setName(fileName); |
| | | mf.setExtName(StaticData.OSGB); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置目录编码 |
| | | */ |
| | | private void setDircode(List<MetaFileEntity> list, DirEntity dir) { |
| | | List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId()); |
| | | for (MetaFileEntity mfe : list) { |
| | | String filePath = mfe.getPath().toLowerCase().replace("/", "\\"); |
| | | String fileName = mfe.getName().toLowerCase(); |
| | | |
| | | String code = findDirByPath(dirs, filePath, fileName); |
| | | if (StringHelper.isEmpty(code)) { |
| | | DirEntity baseDir = findBaseDirByPath(dirs, filePath); |
| | | if (null != baseDir) { |
| | | createDirByPath(baseDir, mfe.getPath().replace("/", "\\")); |
| | | dirs = dirService.selectRecursiveById(dir.getId()); |
| | | code = findDirByPath(dirs, filePath, fileName); |
| | | } |
| | | } |
| | | code = StringHelper.isEmpty(code) ? dir.getCode() : code; |
| | | mfe.setDircode(code); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据路径查找目录 |
| | | */ |
| | | private String findDirByPath(List<DirEntity> dirs, String filePath,String fileName) { |
| | | for (DirEntity de : dirs) { |
| | | if (filePath.contains(de.getFullName().toLowerCase() + "\\" + fileName)) { |
| | | return de.getCode(); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 根据路径查找父目录 |
| | | */ |
| | | private DirEntity findBaseDirByPath(List<DirEntity> dirs, String filePath) { |
| | | for (DirEntity de : dirs) { |
| | | if (StringHelper.isEmpty(de.getCode()) || de.getCode().length() != StaticData.FOUR) { |
| | | continue; |
| | | } |
| | | if (filePath.contains(de.getFullName().toLowerCase() + "\\")) { |
| | | return de; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 根据路径创建目录 |
| | | */ |
| | | private void createDirByPath(DirEntity baseDir, String filePath) { |
| | | int start = filePath.toLowerCase().indexOf(baseDir.getFullName().toLowerCase() + "\\") + baseDir.getFullName().length() + 1; |
| | | int end = filePath.lastIndexOf("\\"); |
| | | if (end <= start) { |
| | | return; |
| | | } |
| | | if (0 == baseDir.getLevel()) { |
| | | baseDir.setLevel(2); |
| | | } |
| | | |
| | | HttpServletRequest req = WebHelper.getRequest(); |
| | | UserEntity ue = tokenService.getCurrentUser(req); |
| | | |
| | | String[] strs = filePath.substring(start, end).split("\\\\"); |
| | | for (String str : strs) { |
| | | DirEntity entity = dirService.selectDirByName(str, baseDir.getId()); |
| | | if (null == entity) { |
| | | entity = new DirEntity(); |
| | | entity.setPid(baseDir.getId()); |
| | | entity.setName(str); |
| | | entity.setOrderNum(dirService.selectMaxOrderNum()); |
| | | entity.setLevel(baseDir.getLevel() + 1); |
| | | entity.setCreateUser(ue.getId()); |
| | | |
| | | dirService.insert(entity); |
| | | baseDir = dirService.selectDir(entity.getId()); |
| | | } else { |
| | | baseDir = entity; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取参数 * |
| | | * Enumeration<String> headers = req.getHeaderNames(); |
| | | * Enumeration<String> attributes = req.getAttributeNames(); |