@@ -18,37 +18,81 @@ export default class PiclistImageHostingService implements ImageHostingService {
1818 }
1919
2020 getId = ( ) => {
21- let uploadUrl = this . config . uploadUrl
22- if ( this . config . key )
23- uploadUrl += `?key=${ this . config . key } `
24- return md5 ( uploadUrl ) // as id
21+ let uploadUrl = this . config . uploadUrl ;
22+ if ( this . config . key ) uploadUrl += `?key=${ this . config . key } ` ;
23+ return md5 ( uploadUrl ) ; // as id
2524 } ;
2625
2726 uploadImage = async ( { data } : UploadImageRequest ) => {
2827 const blob = Base64ImageToBlob ( data ) ;
29- return this . uploadBlob ( blob ) ;
28+ return this . uploadBlob ( blob , `web_cliper_image.png` ) ;
3029 } ;
3130
3231 uploadImageUrl = async ( url : string ) => {
3332 const imageBlob = await Container . get ( IBasicRequestService ) . download ( url ) ;
34- return this . uploadBlob ( imageBlob ) ;
33+ return this . uploadBlob ( imageBlob , this . _getImageFileName ( url ) ) ;
3534 } ;
3635
37- private uploadBlob = async ( blob : Blob ) : Promise < string > => {
36+ private uploadBlob = async ( blob : Blob , fileName ?: string ) : Promise < string > => {
3837 const request = new RequestHelper ( { request : Container . get ( IBasicRequestService ) } ) ;
39- let uploadUrl = this . config . uploadUrl
40- if ( this . config . key )
41- uploadUrl += `?key=${ this . config . key } `
38+ let uploadUrl = this . config . uploadUrl ;
39+ if ( this . config . key ) uploadUrl += `?key=${ this . config . key } ` ;
4240 let formData = new FormData ( ) ;
43- formData . append ( 'image' , blob ) ;
44- let result = await request . postForm < { success : boolean , result : string [ ] } > (
45- uploadUrl ,
46- {
47- data : formData ,
48- }
49- ) ;
50- if ( ! result . success )
51- throw new Error ( "Upload failed" ) ;
41+ formData . append ( 'image' , blob , fileName ) ;
42+ let result = await request . postForm < { success : boolean ; result : string [ ] } > ( uploadUrl , {
43+ data : formData ,
44+ } ) ;
45+ if ( ! result . success ) throw new Error ( 'Upload failed' ) ;
5246 return result . result [ 0 ] ;
5347 } ;
48+ private _getImageFileName ( url : string ) {
49+ // 分割路径和查询参数
50+ const queryIndex = url . indexOf ( '?' ) ;
51+ const pathPart = queryIndex === - 1 ? url : url . slice ( 0 , queryIndex ) ;
52+ const queryPart = queryIndex === - 1 ? '' : url . slice ( queryIndex + 1 ) ;
53+
54+ // 处理路径部分
55+ const segments = pathPart . split ( '/' ) ;
56+ let lastSegment = segments . pop ( ) || '' ;
57+
58+ // 移除可能的哈希片段
59+ const hashIndex = lastSegment . indexOf ( '#' ) ;
60+ if ( hashIndex !== - 1 ) {
61+ lastSegment = lastSegment . slice ( 0 , hashIndex ) ;
62+ }
63+
64+ // 检查最后一段是否为文件名
65+ if ( lastSegment . includes ( '.' ) ) {
66+ return lastSegment ;
67+ }
68+ let fileName = "web_cliper_image"
69+ let fileExt : string = "png" ;
70+ // 解析查询参数中的后缀
71+ const queryParams = new URLSearchParams ( queryPart ) ;
72+ const formatKeys = [ 'wx_fmt' , 'format' , 'fm' , 'type' ] ;
73+ for ( const key of formatKeys ) {
74+ if ( queryParams . has ( key ) ) {
75+ fileExt = queryParams . get ( key ) as string ;
76+ if ( fileExt ) {
77+ break ;
78+ }
79+ }
80+ }
81+
82+ // 检查路径中的其他段是否有已知图片后缀
83+ const imageExts = [ 'png' , 'jpg' , 'jpeg' , 'gif' , 'webp' , 'bmp' ] ;
84+ for ( const seg of segments ) {
85+ const dotIndex = seg . lastIndexOf ( '.' ) ;
86+ if ( dotIndex !== - 1 ) {
87+ const ext = seg . slice ( dotIndex + 1 ) . toLowerCase ( ) ;
88+ if ( imageExts . includes ( ext ) ) {
89+ fileExt = ext ;
90+ break ;
91+ }
92+ }
93+ }
94+
95+ // 默认返回空字符串
96+ return `${ fileName } .${ fileExt } ` ;
97+ }
5498}
0 commit comments