@@ -9,10 +9,12 @@ import { CommonTranslationKey, en, Namespace, zh } from '../common/i18n'
99import { confirm } from '../common/notification'
1010import { legacyFileSave } from '../common/legacy'
1111import { reportBug } from '../common/issue'
12- import { withSignal } from '../common/utils'
12+ import { UniqueFileName , withSignal } from '../common/utils'
1313import { getDownloadSettings } from '../common/settings'
1414import { DownloadMethod , SettingKey } from '@/common/settings'
1515
16+ const uniqueFileName = new UniqueFileName ( )
17+
1618const DOWNLOAD_ABORTED = 'Download aborted'
1719
1820const enum TranslationKey {
@@ -85,32 +87,6 @@ i18next
8587 } )
8688 . catch ( console . error )
8789
88- const usedNames = new Set < string > ( )
89- const fileNameToPreId = new Map < string , number > ( )
90- const uniqueFileName = ( originFileName : string ) => {
91- if ( usedNames . has ( originFileName ) ) {
92- const startDotIndex = originFileName . lastIndexOf ( '.' )
93-
94- const preId = fileNameToPreId . get ( originFileName ) ?? 0
95- const id = preId + 1
96- fileNameToPreId . set ( originFileName , id )
97-
98- const fileName =
99- startDotIndex === - 1
100- ? originFileName . concat ( `-${ id . toFixed ( ) } ` )
101- : originFileName
102- . slice ( 0 , startDotIndex )
103- . concat ( `-${ id . toFixed ( ) } ` )
104- . concat ( originFileName . slice ( startDotIndex ) )
105-
106- return fileName
107- }
108-
109- usedNames . add ( originFileName )
110-
111- return originFileName
112- }
113-
11490interface ProgressOptions {
11591 onProgress ?: ( progress : number ) => void
11692 onComplete ?: ( ) => void
@@ -186,7 +162,7 @@ const downloadImage = async (
186162 const content = await fetchBlob ( )
187163 if ( ! content ) return null
188164
189- const name = uniqueFileName ( 'diagram.png' )
165+ const name = uniqueFileName . generate ( 'diagram.png' )
190166 const filename = `images/${ name } `
191167
192168 image . url = filename
@@ -205,7 +181,7 @@ const downloadImage = async (
205181 const sources = await fetchSources ( )
206182 if ( ! sources ) return null
207183
208- const name = uniqueFileName ( originName )
184+ const name = uniqueFileName . generate ( originName )
209185 const filename = `images/${ name } `
210186
211187 const { src } = sources
@@ -300,7 +276,7 @@ const downloadFile = async (
300276 const result = await withSignal (
301277 async ( ) => {
302278 try {
303- const filename = `files/${ uniqueFileName ( name ) } `
279+ const filename = `files/${ uniqueFileName . generate ( name ) } `
304280
305281 const response = await fetchFile ( { signal : controller . signal } )
306282 try {
0 commit comments