@@ -125,6 +125,8 @@ import getFileList from '../services/FileList'
125125import getFileInfo from ' ../services/FileInfo'
126126import PreviewUrl from ' ../mixins/PreviewUrl'
127127
128+ import cancelableRequest from ' ../utils/CancelableRequest'
129+
128130export default {
129131 name: ' Viewer' ,
130132
@@ -154,6 +156,10 @@ export default {
154156 isLoaded: false ,
155157 initiated: false ,
156158
159+ // cancellable requests
160+ cancelRequestFile : () => {},
161+ cancelRequestFolder : () => {},
162+
157163 shownSidebar: false ,
158164 sidebarWidth: 0 ,
159165
@@ -240,13 +246,21 @@ export default {
240246 * @param {string} path the file path to open
241247 */
242248 async openFile (path ) {
249+ // cancel any previous requests
250+ this .cancelRequestFile ()
251+ this .cancelRequestFolder ()
252+
243253 // do not open the same file again
244254 if (path === this .currentFile .path ) {
245255 return
246256 }
247257
248258 // initial loading start
249259 this .initiated = true
260+ const { request: fileRequest , cancel: cancelRequestFile } = cancelableRequest (getFileInfo)
261+ const { request: folderRequest , cancel: cancelRequestFolder } = cancelableRequest (getFileList)
262+ this .cancelRequestFile = cancelRequestFile
263+ this .cancelRequestFolder = cancelRequestFolder
250264
251265 // extcrat needed info from path
252266 const [dirPath , fileName ] = extractFilePaths (path)
@@ -261,53 +275,57 @@ export default {
261275 document .title = ` ${ fileName} - ${ OC .theme .title } `
262276 }
263277
264- // retrieve, sort and store file List
265- let fileInfo = await getFileInfo (path)
278+ try {
266279
267- // get original mime
268- let mime = fileInfo . mime
280+ // retrieve, sort and store file List
281+ let fileInfo = await fileRequest (path)
269282
270- // check if part of a group, if so retrieve full files list
271- const group = this .mimeGroups [mime]
272- if (group) {
273- const mimes = this .mimeGroups [group]
274- ? this .mimeGroups [group]
275- : [mime]
283+ // get original mime
284+ let mime = fileInfo .mime
276285
277- // retrieve folder list
278- const fileList = await getFileList (dirPath)
286+ // check if part of a group, if so retrieve full files list
287+ const group = this .mimeGroups [mime]
288+ if (group) {
289+ const mimes = this .mimeGroups [group]
290+ ? this .mimeGroups [group]
291+ : [mime]
279292
280- // filter out the unwanted mimes
281- const filteredFiles = fileList .filter (file => file .mime && mimes .indexOf (file .mime ) !== - 1 )
282- console .info (filteredFiles)
293+ // retrieve folder list
294+ const fileList = await folderRequest (dirPath)
283295
284- // sort like the files list
285- // TODO: implement global sorting API
286- // https://github.com/nextcloud/server/blob/a83b79c5f8ab20ed9b4d751167417a65fa3c42b8/apps/files/lib/Controller/ApiController.php#L247
287- this .fileList = filteredFiles .sort ((a , b ) => sortCompare (a, b, ' basename' ))
296+ // filter out the unwanted mimes
297+ const filteredFiles = fileList .filter (file => file .mime && mimes .indexOf (file .mime ) !== - 1 )
288298
289- // store current position
290- this .currentIndex = this .fileList .findIndex (file => file .basename === fileName)
291- } else {
292- this .currentIndex = 0
293- this .fileList = [fileInfo]
294- }
299+ // sort like the files list
300+ // TODO: implement global sorting API
301+ // https://github.com/nextcloud/server/blob/a83b79c5f8ab20ed9b4d751167417a65fa3c42b8/apps/files/lib/Controller/ApiController.php#L247
302+ this .fileList = filteredFiles .sort ((a , b ) => sortCompare (a, b, ' basename' ))
295303
296- // get saved fileInfo
297- fileInfo = this .fileList [this .currentIndex ]
304+ // store current position
305+ this .currentIndex = this .fileList .findIndex (file => file .basename === fileName)
306+ } else {
307+ this .currentIndex = 0
308+ this .fileList = [fileInfo]
309+ }
298310
299- // override mimetype if existing alias
300- if (! this .components [mime]) {
301- mime = mime .split (' /' )[0 ]
302- }
311+ // get saved fileInfo
312+ fileInfo = this .fileList [this .currentIndex ]
303313
304- // if we have a valid mime, show it!
305- if (this .components [mime]) {
306- this .currentFile = new File (fileInfo, mime, this .components [mime])
307- this .updatePreviousNext ()
308- } else {
309- console .error (` The following file could not be displayed` , fileName, fileInfo)
310- this .close ()
314+ // override mimetype if existing alias
315+ if (! this .components [mime]) {
316+ mime = mime .split (' /' )[0 ]
317+ }
318+
319+ // if we have a valid mime, show it!
320+ if (this .components [mime]) {
321+ this .currentFile = new File (fileInfo, mime, this .components [mime])
322+ this .updatePreviousNext ()
323+ } else {
324+ console .error (` The following file could not be displayed` , fileName, fileInfo)
325+ this .close ()
326+ }
327+ } catch (error) {
328+ console .error (error)
311329 }
312330 },
313331
@@ -488,6 +506,10 @@ export default {
488506 this .initiated = false
489507 this .hideAppsSidebar ()
490508
509+ // cancel requests
510+ this .cancelRequestFile ()
511+ this .cancelRequestFolder ()
512+
491513 // restore default
492514 document .body .style .overflow = null
493515
0 commit comments