@@ -114,16 +114,30 @@ class ShareService {
114114 . replace ( "[param2]" , "[" + docId + "]" )
115115 this . addLog ( successMsg , "info" )
116116
117- // 处理图片
117+ // 处理图片和DataViews媒体资源
118118 const data = resp . data
119+
120+ // 处理常规媒体资源
119121 const media = data . media
120122 if ( media && media . length > 0 ) {
121123 showMessage ( this . pluginInstance . i18n [ "shareService" ] [ "msgProcessPic" ] , 7000 , "info" )
122124 // 异步处理图片
123125 this . addLog ( this . pluginInstance . i18n [ "shareService" ] [ "msgStartPicBack" ] , "info" )
124126 void this . processShareMedia ( docId , media )
125127 this . addLog ( this . pluginInstance . i18n [ "shareService" ] [ "msgEndPicBack" ] , "info" )
126- } else {
128+ }
129+
130+ // 处理数据库媒体资源
131+ const dataViewMedia = data . dataViewMedia
132+ if ( dataViewMedia && dataViewMedia . length > 0 ) {
133+ showMessage ( this . pluginInstance . i18n [ "shareService" ] [ "msgProcessPic" ] , 7000 , "info" )
134+ // 异步处理数据库媒体资源
135+ this . addLog ( this . pluginInstance . i18n [ "shareService" ] [ "msgStartDataViewMediaBack" ] , "info" )
136+ void this . processDataViewMedia ( docId , dataViewMedia )
137+ this . addLog ( this . pluginInstance . i18n [ "shareService" ] [ "msgEndDataViewMediaBack" ] , "info" )
138+ }
139+
140+ if ( ! media || media . length === 0 ) {
127141 showMessage ( this . pluginInstance . i18n [ "shareService" ] [ "msgShareSuccess" ] , 3000 , "info" )
128142 }
129143 } catch ( e ) {
@@ -355,6 +369,147 @@ class ShareService {
355369 }
356370 }
357371
372+ /**
373+ * 处理数据库媒体资源
374+ * @param docId 文档ID
375+ * @param mediaList 数据库媒体资源列表
376+ */
377+ private async processDataViewMedia ( docId : string , mediaList : any [ ] ) {
378+ const processingMsg = this . pluginInstance . i18n [ "shareService" ] [ "processingDataViewMedia" ] + ":" + docId
379+ this . addLog ( processingMsg , "info" )
380+ const { cfg } = await ApiUtils . getSiyuanKernelApi ( this . pluginInstance )
381+
382+ const perReq = 5
383+ const groupedMedia = [ ]
384+ for ( let i = 0 ; i < mediaList . length ; i += perReq ) {
385+ groupedMedia . push ( mediaList . slice ( i , i + perReq ) )
386+ }
387+
388+ let errorCount = 0
389+ let successCount = 0
390+ let totalCount = 0
391+ for ( let i = 0 ; i < groupedMedia . length ; i ++ ) {
392+ const mediaGroup = groupedMedia [ i ]
393+ const processedParams = [ ]
394+
395+ const msgStartGroup = this . pluginInstance . i18n [ "shareService" ] [ "msgStartGroup" ]
396+ const msgStartGroupWithParam = msgStartGroup
397+ . replace ( "[param1]" , i + 1 )
398+ . replace ( "[param2]" , groupedMedia . length )
399+ . replace ( "[param3]" , perReq )
400+ this . addLog ( msgStartGroupWithParam , "info" )
401+ for ( const media of mediaGroup ) {
402+ try {
403+ totalCount += 1
404+
405+ const originalUrl = media . originalUrl ?? ""
406+ let imageUrl = originalUrl
407+ const alt = media . alt
408+ const title = media . title
409+
410+ if ( ! imageUrl . startsWith ( "http" ) ) {
411+ if ( cfg . serviceApiConfig . apiUrl . endsWith ( "/" ) ) {
412+ imageUrl = cfg . siyuanConfig . apiUrl + imageUrl
413+ } else {
414+ imageUrl = cfg . siyuanConfig . apiUrl + "/" + imageUrl
415+ }
416+ }
417+
418+ const msgStartCurrentPic = this . pluginInstance . i18n [ "shareService" ] [ "msgStartCurrentDataViewMedia" ]
419+ const msgStartCurrentPicWithParam = msgStartCurrentPic
420+ . replace ( "[param1]" , totalCount )
421+ . replace ( "[param2]" , imageUrl )
422+ this . addLog ( msgStartCurrentPicWithParam , "info" )
423+
424+ const res = await ImageUtils . fetchBase64WithContentType ( imageUrl )
425+ this . addLog ( `DataView image base64 response =>${ res } ` , "info" )
426+
427+ if ( res ?. status !== 200 ) {
428+ errorCount += 1
429+ this . addLog ( `DataView image retrieval error: ${ res . msg } ` , "error" )
430+ continue
431+ }
432+
433+ const base64 = res . body
434+ const type = res . contentType
435+ const params = {
436+ file : base64 ,
437+ originalUrl : originalUrl ,
438+ alt : alt ,
439+ title : title ,
440+ type : type ,
441+ source : "dataviews" // 标识资源来源为DataView
442+ }
443+ processedParams . push ( params )
444+ } catch ( e ) {
445+ const mediaErrorMsg = this . pluginInstance . i18n [ "shareService" ] [ "msgDataViewMediaUploadError" ] + e
446+ this . addLog ( mediaErrorMsg , "error" )
447+ showMessage ( this . pluginInstance . i18n [ "shareService" ] [ "msgDataViewMediaUploadError" ] + e , 7000 , "error" )
448+ }
449+ }
450+ const msgGroupProcessSuccess = this . pluginInstance . i18n [ "shareService" ] [ "msgGroupProcessSuccess" ]
451+ const msgGroupProcessSuccessWithParam = msgGroupProcessSuccess
452+ . replace ( "[param1]" , i + 1 )
453+ . replace ( "[param2]" , groupedMedia . length )
454+ . replace ( "[param3]" , perReq )
455+ this . addLog ( msgGroupProcessSuccessWithParam , "info" )
456+
457+ const hasNext = i < groupedMedia . length - 1
458+ const reqParams = {
459+ docId : docId ,
460+ medias : processedParams ,
461+ hasNext : hasNext ,
462+ }
463+
464+ // 处理上传结果
465+ const msgProcessPicBatch = this . pluginInstance . i18n [ "shareService" ] [ "msgProcessDataViewMediaBatch" ]
466+ const msgProcessPicBatchWithParam = msgProcessPicBatch . replace ( "[param1]" , i + 1 )
467+ this . addLog ( msgProcessPicBatchWithParam , "info" )
468+ const uploadResult = await this . shareApi . uploadDataViewMedia ( reqParams )
469+ this . addLog ( this . pluginInstance . i18n [ "shareService" ] [ "msgBatchResult" ] + JSON . stringify ( uploadResult ) , "info" )
470+ if ( uploadResult . code === 0 ) {
471+ successCount += processedParams . length
472+ if ( ! hasNext ) {
473+ showMessage (
474+ this . pluginInstance . i18n [ "shareService" ] [ "msgYourDoc" ] +
475+ docId +
476+ this . pluginInstance . i18n [ "shareService" ] [ "msgSuccessUpdateDataViewMedia" ] ,
477+ 3000 ,
478+ "info"
479+ )
480+ }
481+ const msgCurrentMediaSuccess = this . pluginInstance . i18n [ "shareService" ] [ "msgCurrentDataViewMediaSuccess" ]
482+ const msgCurrentMediaSuccessWithParam = msgCurrentMediaSuccess . replace ( "[param1]" , i + 1 )
483+ this . addLog ( msgCurrentMediaSuccessWithParam , "info" )
484+ } else {
485+ errorCount += processedParams . length
486+ let rtnMsg = uploadResult . msg
487+ if ( ! uploadResult . msg ) {
488+ rtnMsg = ( uploadResult as any ) . message
489+ }
490+ const msgCurrentMediaError = this . pluginInstance . i18n [ "shareService" ] [ "msgCurrentDataViewMediaError" ]
491+ const msgCurrentMediaErrorWithParam = msgCurrentMediaError . replace ( "[param1]" , i + 1 )
492+ const errMsg = msgCurrentMediaErrorWithParam + rtnMsg
493+ this . addLog ( errMsg , "error" )
494+ showMessage ( errMsg , 7000 , "error" )
495+ }
496+ }
497+
498+ const successPic = this . pluginInstance . i18n [ "shareService" ] [ "successDataViewMedia" ]
499+ const successPicWithParam = successPic
500+ . replace ( "[param1]" , totalCount )
501+ . replace ( "[param2]" , successCount )
502+ . replace ( "[param3]" , errorCount )
503+ this . addLog ( successPicWithParam , "info" )
504+ if ( successCount === totalCount ) {
505+ showMessage ( this . pluginInstance . i18n [ "shareService" ] [ "success" ] , 3000 , "info" )
506+ } else {
507+ const errorPic = this . pluginInstance . i18n [ "shareService" ] [ "errorDataViewMedia" ]
508+ const msgWithParam = errorPic . replace ( "[param1]" , errorCount )
509+ showMessage ( msgWithParam , 7000 , "error" )
510+ }
511+ }
512+
358513 private addLog ( msg : string , type : "info" | "error" ) {
359514 updateStatusBar ( this . pluginInstance , msg )
360515 if ( type === "info" ) {
0 commit comments