@@ -53,7 +53,7 @@ const getExt = type => {
5353 }
5454}
5555
56- const checkList = ( list , musicInfo , type ) => list . some ( s => s . musicInfo . songmid === musicInfo . songmid && s . type === type )
56+ const checkList = ( list , musicInfo , type , ext ) => list . some ( s => s . musicInfo . songmid === musicInfo . songmid && ( s . type === type || s . ext === ext ) )
5757
5858const getStartTask = ( list , downloadStatus , maxDownloadNum ) => {
5959 let downloadCount = 0
@@ -121,11 +121,24 @@ const downloadLyric = (downloadInfo, filePath) => {
121121 } )
122122}
123123
124+ const refreshUrl = function ( commit , downloadInfo ) {
125+ commit ( 'setStatusText' , { downloadInfo, text : '链接失效,正在刷新链接' } )
126+ getUrl ( downloadInfo , true ) . then ( result => {
127+ commit ( 'updateUrl' , { downloadInfo, url : result . url } )
128+ commit ( 'setStatusText' , { downloadInfo, text : '链接刷新成功' } )
129+ dls [ downloadInfo . key ] . refreshUrl ( result . url )
130+ dls [ downloadInfo . key ] . start ( )
131+ } ) . catch ( err => {
132+ console . log ( err )
133+ this . dispatch ( 'download/startTask' )
134+ } )
135+ }
136+
124137// actions
125138const actions = {
126139 createDownload ( { state, rootState, commit } , { musicInfo, type } ) {
127- if ( checkList ( state . list , musicInfo , type ) ) return
128140 let ext = getExt ( type )
141+ if ( checkList ( state . list , musicInfo , type , ext ) ) return
129142 const downloadInfo = {
130143 isComplate : false ,
131144 status : state . downloadStatus . WAITING ,
@@ -146,6 +159,11 @@ const actions = {
146159 }
147160 downloadInfo . filePath = path . join ( rootState . setting . download . savePath , downloadInfo . fileName )
148161 commit ( 'addTask' , downloadInfo )
162+ try { // 删除同路径下的同名文件
163+ fs . unlinkSync ( downloadInfo . filePath )
164+ } catch ( err ) {
165+ if ( err . code !== 'ENOENT' ) return commit ( 'setStatusText' , { downloadInfo, text : '文件删除失败' } )
166+ }
149167 if ( dls [ downloadInfo . key ] ) {
150168 dls [ downloadInfo . key ] . stop ( ) . finally ( ( ) => {
151169 delete dls [ downloadInfo . key ]
@@ -167,7 +185,7 @@ const actions = {
167185 if ( ! downloadInfo ) downloadInfo = result
168186
169187 // 开始任务
170- commit ( 'onDownload ' , downloadInfo )
188+ commit ( 'onStart ' , downloadInfo )
171189 commit ( 'setStatusText' , { downloadInfo, text : '任务初始化中' } )
172190 let msg = checkPath ( rootState . setting . download . savePath )
173191 if ( msg ) return commit ( 'setStatusText' , '检查下载目录出错: ' + msg )
@@ -178,12 +196,12 @@ const actions = {
178196 fileName : downloadInfo . fileName ,
179197 method : 'get' ,
180198 override : true ,
181- onEnd ( ) {
182- if ( downloadInfo . progress . progress != '100.00' ) {
183- delete dls [ downloadInfo . key ]
184- return this . dispatch ( 'download/startTask' , downloadInfo )
185- }
186- commit ( 'onEnd ' , downloadInfo )
199+ onCompleted ( ) {
200+ // if (downloadInfo.progress.progress != '100.00') {
201+ // delete dls[downloadInfo.key]
202+ // return this.dispatch('download/startTask', downloadInfo)
203+ // }
204+ commit ( 'onCompleted ' , downloadInfo )
187205 _this . dispatch ( 'download/startTask' )
188206 const filePath = path . join ( options . path , options . fileName )
189207
@@ -199,54 +217,40 @@ const actions = {
199217 _this . dispatch ( 'download/startTask' )
200218 return
201219 }
202- let code
203- if ( err . message . includes ( 'Response status was' ) ) {
204- code = err . message . replace ( / R e s p o n s e s t a t u s w a s ( \d + ) $ / , '$1' )
205- } else if ( err . code === 'ETIMEDOUT' || err . code == 'ENOTFOUND' ) {
206- code = err . code
220+ if ( err . code == 'ENOTFOUND' ) {
221+ refreshUrl . call ( _this , commit , downloadInfo )
207222 } else {
208223 console . log ( 'Download failed, Attempting Retry' )
209- dls [ downloadInfo . key ] . resume ( )
224+ dls [ downloadInfo . key ] . start ( )
210225 commit ( 'setStatusText' , { downloadInfo, text : '正在重试' } )
226+ }
227+ } ,
228+ onFail ( response ) {
229+ commit ( 'onError' , downloadInfo )
230+
231+ if ( ++ tryNum [ downloadInfo . key ] > 2 ) {
232+ _this . dispatch ( 'download/startTask' )
211233 return
212234 }
213- switch ( code ) {
214- case '401' :
215- case '403' :
216- case '410' :
217- case 'ETIMEDOUT' :
218- case 'ENOTFOUND' :
219- commit ( 'setStatusText' , { downloadInfo, text : '链接失效,正在刷新链接' } )
220- getUrl ( downloadInfo , true ) . then ( result => {
221- commit ( 'updateUrl' , { downloadInfo, url : result . url } )
222- commit ( 'setStatusText' , { downloadInfo, text : '链接刷新成功' } )
223- dls [ downloadInfo . key ] . url = dls [ downloadInfo . key ] . requestURL = result . url
224- dls [ downloadInfo . key ] . __initProtocol ( result . url )
225- dls [ downloadInfo . key ] . resume ( )
226- } ) . catch ( err => {
227- console . log ( err )
228- _this . dispatch ( 'download/startTask' )
229- } )
235+ switch ( response . statusCode ) {
236+ case 401 :
237+ case 403 :
238+ case 410 :
239+ refreshUrl . call ( _this , commit , downloadInfo )
230240 }
231241 } ,
232- // onStateChanged(state) {
233- // console.log(state)
234- // },
235- onDownload ( ) {
236- commit ( 'onDownload' , downloadInfo )
237- console . log ( 'on download' )
242+ onStart ( ) {
243+ commit ( 'onStart' , downloadInfo )
244+ console . log ( 'on start' )
238245 } ,
239246 onProgress ( status ) {
240247 commit ( 'onProgress' , { downloadInfo, status } )
241248 console . log ( status )
242249 } ,
243- onPause ( ) {
250+ onStop ( ) {
244251 commit ( 'pauseTask' , downloadInfo )
245252 _this . dispatch ( 'download/startTask' )
246253 } ,
247- onResume ( ) {
248- commit ( 'resumeTask' , downloadInfo )
249- } ,
250254 }
251255 commit ( 'setStatusText' , { downloadInfo, text : '获取URL中...' } )
252256 let p = options . url ? Promise . resolve ( ) : getUrl ( downloadInfo ) . then ( result => {
@@ -315,9 +319,6 @@ const mutations = {
315319 downloadInfo . status = state . downloadStatus . PAUSE
316320 downloadInfo . statusText = '暂停下载'
317321 } ,
318- resumeTask ( state , downloadInfo ) {
319- downloadInfo . statusText = '开始下载'
320- } ,
321322 setStatusText ( state , { downloadInfo, index, text } ) { // 设置状态文本
322323 if ( downloadInfo ) {
323324 downloadInfo . statusText = text
@@ -352,7 +353,7 @@ const mutations = {
352353 state . list [ index ] . status = status
353354 }
354355 } ,
355- onEnd ( state , downloadInfo ) {
356+ onCompleted ( state , downloadInfo ) {
356357 downloadInfo . isComplate = true
357358 downloadInfo . status = state . downloadStatus . COMPLETED
358359 downloadInfo . statusText = '下载完成'
@@ -361,7 +362,7 @@ const mutations = {
361362 downloadInfo . status = state . downloadStatus . ERROR
362363 downloadInfo . statusText = '任务出错'
363364 } ,
364- onDownload ( state , downloadInfo ) {
365+ onStart ( state , downloadInfo ) {
365366 downloadInfo . status = state . downloadStatus . RUN
366367 downloadInfo . statusText = '正在下载'
367368 } ,
0 commit comments