@@ -22,71 +22,99 @@ public class FileMetadataService {
2222
2323 /**
2424 * 扫描文件路径列表,提取文件元数据
25- * @param filePaths 文件路径列表
2625 * @param datasetId 数据集ID
2726 * @return 数据集文件列表
2827 */
2928 public List <DatasetFile > scanFiles (List <String > filePaths , String datasetId ) {
3029 List <DatasetFile > datasetFiles = new ArrayList <>();
31-
30+
3231 if (filePaths == null || filePaths .isEmpty ()) {
3332 log .warn ("文件路径列表为空,跳过扫描" );
3433 return datasetFiles ;
3534 }
36-
35+
3736 for (String filePath : filePaths ) {
3837 try {
39- DatasetFile datasetFile = extractFileMetadata (filePath , datasetId );
40- if (datasetFile != null ) {
41- datasetFiles .add (datasetFile );
38+ Path path = Paths .get (filePath );
39+
40+ if (!Files .exists (path )) {
41+ log .warn ("路径不存在: {}" , filePath );
42+ continue ;
43+ }
44+
45+ if (Files .isDirectory (path )) {
46+ scanDirectory (datasetId , filePath , path , datasetFiles );
47+ } else {
48+ // 如果是文件,直接处理
49+ DatasetFile datasetFile = extractFileMetadata (filePath , datasetId );
50+ if (datasetFile != null ) {
51+ datasetFiles .add (datasetFile );
52+ }
4253 }
4354 } catch (Exception e ) {
44- log .error ("扫描文件失败 : {}, 错误: {}" , filePath , e .getMessage (), e );
55+ log .error ("扫描路径失败 : {}, 错误: {}" , filePath , e .getMessage (), e );
4556 }
4657 }
47-
58+
4859 log .info ("文件扫描完成,共扫描 {} 个文件" , datasetFiles .size ());
4960 return datasetFiles ;
5061 }
51-
62+
63+ private void scanDirectory (String datasetId , String filePath , Path path , List <DatasetFile > datasetFiles ) throws IOException {
64+ // 如果是目录,扫描该目录下的所有文件(非递归)
65+ List <Path > filesInDir = Files .list (path )
66+ .filter (Files ::isRegularFile )
67+ .toList ();
68+
69+ for (Path file : filesInDir ) {
70+ try {
71+ DatasetFile datasetFile = extractFileMetadata (file .toString (), datasetId );
72+ if (datasetFile != null ) {
73+ datasetFiles .add (datasetFile );
74+ }
75+ } catch (Exception e ) {
76+ log .error ("处理目录中的文件失败: {}, 错误: {}" , file , e .getMessage (), e );
77+ }
78+ }
79+ log .info ("已扫描目录 {} 下的 {} 个文件" , filePath , filesInDir .size ());
80+ }
81+
5282 /**
53- * 提取单个文件的元数据
5483 * @param filePath 文件路径
5584 * @param datasetId 数据集ID
5685 * @return 数据集文件对象
5786 */
5887 private DatasetFile extractFileMetadata (String filePath , String datasetId ) throws IOException {
5988 Path path = Paths .get (filePath );
60-
89+
6190 if (!Files .exists (path )) {
6291 log .warn ("文件不存在: {}" , filePath );
6392 return null ;
6493 }
65-
94+
6695 if (!Files .isRegularFile (path )) {
6796 log .warn ("路径不是文件: {}" , filePath );
6897 return null ;
6998 }
70-
99+
71100 String fileName = path .getFileName ().toString ();
72101 long fileSize = Files .size (path );
73- String fileFormat = getFileExtension (fileName );
74- String fileType = determineFileType (fileFormat );
75-
102+ String fileType = getFileExtension (fileName );
103+
76104 return DatasetFile .builder ()
77105 .id (UUID .randomUUID ().toString ())
78106 .datasetId (datasetId )
79107 .fileName (fileName )
80108 .filePath (filePath )
81109 .fileSize (fileSize )
82- .fileFormat (fileFormat )
110+ .fileFormat (fileType )
83111 .fileType (fileType )
84112 .uploadTime (LocalDateTime .now ())
85113 .lastAccessTime (LocalDateTime .now ())
86114 .status ("UPLOADED" )
87115 .build ();
88116 }
89-
117+
90118 /**
91119 * 获取文件扩展名
92120 */
@@ -97,40 +125,4 @@ private String getFileExtension(String fileName) {
97125 }
98126 return "unknown" ;
99127 }
100-
101- /**
102- * 根据文件扩展名判断文件类型
103- */
104- private String determineFileType (String fileFormat ) {
105- if (fileFormat == null ) {
106- return "UNKNOWN" ;
107- }
108-
109- // 图片类型
110- if (fileFormat .matches ("jpg|jpeg|png|gif|bmp|webp|svg|tiff|ico" )) {
111- return "IMAGE" ;
112- }
113-
114- // 视频类型
115- if (fileFormat .matches ("mp4|avi|mov|wmv|flv|mkv|webm|m4v" )) {
116- return "VIDEO" ;
117- }
118-
119- // 音频类型
120- if (fileFormat .matches ("mp3|wav|flac|aac|ogg|wma|m4a" )) {
121- return "AUDIO" ;
122- }
123-
124- // 文本类型
125- if (fileFormat .matches ("txt|md|json|xml|csv|log|yaml|yml|properties|conf" )) {
126- return "TEXT" ;
127- }
128-
129- // 文档类型
130- if (fileFormat .matches ("pdf|doc|docx|xls|xlsx|ppt|pptx" )) {
131- return "DOCUMENT" ;
132- }
133-
134- return "OTHER" ;
135- }
136128}
0 commit comments