Bläddra i källkod

Merge pull request #723 from transloadit/fix/restore-thumbnail

Regenerate thumbnails after restore.
Artur Paikin 7 år sedan
förälder
incheckning
bad0cfdd47

+ 0 - 2
src/plugins/GoldenRetriever/index.js

@@ -170,8 +170,6 @@ module.exports = class GoldenRetriever extends Plugin {
       }
       const updatedFile = Object.assign({}, originalFile, updatedFileData)
       updatedFiles[fileID] = updatedFile
-
-      this.uppy.generatePreview(updatedFile)
     })
 
     this.uppy.setState({

+ 15 - 0
src/plugins/ThumbnailGenerator/index.js

@@ -21,6 +21,7 @@ module.exports = class ThumbnailGenerator extends Plugin {
     this.opts = Object.assign({}, defaultOptions, opts)
 
     this.addToQueue = this.addToQueue.bind(this)
+    this.onRestored = this.onRestored.bind(this)
   }
 
   /**
@@ -194,10 +195,24 @@ module.exports = class ThumbnailGenerator extends Plugin {
     return Promise.resolve()
   }
 
+  onRestored () {
+    const fileIDs = Object.keys(this.uppy.getState().files)
+    fileIDs.forEach((fileID) => {
+      const file = this.uppy.getFile(fileID)
+      if (!file.isRestored) return
+      // Only add blob URLs; they are likely invalid after being restored.
+      if (!file.preview || /^blob:/.test(file.preview)) {
+        this.addToQueue(file)
+      }
+    })
+  }
+
   install () {
     this.uppy.on('file-added', this.addToQueue)
+    this.uppy.on('restored', this.onRestored)
   }
   uninstall () {
     this.uppy.off('file-added', this.addToQueue)
+    this.uppy.off('restored', this.onRestored)
   }
 }

+ 54 - 3
src/plugins/ThumbnailGenerator/index.test.js

@@ -1,5 +1,6 @@
 const ThumbnailGeneratorPlugin = require('./index')
 const Plugin = require('../../core/Plugin')
+const emitter = require('namespace-emitter')
 
 const delay = duration => new Promise(resolve => setTimeout(resolve, duration))
 
@@ -27,7 +28,7 @@ describe('uploader/ThumbnailGeneratorPlugin', () => {
       plugin.addToQueue = jest.fn()
       plugin.install()
 
-      expect(core.on).toHaveBeenCalledTimes(1)
+      expect(core.on).toHaveBeenCalledTimes(2)
       expect(core.on).toHaveBeenCalledWith('file-added', plugin.addToQueue)
     })
   })
@@ -43,11 +44,11 @@ describe('uploader/ThumbnailGeneratorPlugin', () => {
       plugin.addToQueue = jest.fn()
       plugin.install()
 
-      expect(core.on).toHaveBeenCalledTimes(1)
+      expect(core.on).toHaveBeenCalledTimes(2)
 
       plugin.uninstall()
 
-      expect(core.off).toHaveBeenCalledTimes(1)
+      expect(core.off).toHaveBeenCalledTimes(2)
       expect(core.off).toHaveBeenCalledWith('file-added', plugin.addToQueue)
     })
   })
@@ -347,4 +348,54 @@ describe('uploader/ThumbnailGeneratorPlugin', () => {
       })
     })
   })
+
+  describe('onRestored', () => {
+    it('should enqueue restored files', () => {
+      const files = {
+        a: { preview: 'blob:abc', isRestored: true },
+        b: { preview: 'blob:def' },
+        c: { preview: 'blob:xyz', isRestored: true }
+      }
+      const core = Object.assign(emitter(), {
+        getState () {
+          return { files }
+        },
+        getFile (id) {
+          return files[id]
+        }
+      })
+
+      const plugin = new ThumbnailGeneratorPlugin(core)
+      plugin.addToQueue = jest.fn()
+      plugin.install()
+
+      core.emit('restored')
+
+      expect(plugin.addToQueue).toHaveBeenCalledTimes(2)
+      expect(plugin.addToQueue).toHaveBeenCalledWith(files.a)
+      expect(plugin.addToQueue).toHaveBeenCalledWith(files.c)
+    })
+
+    it('should not regenerate thumbnail for remote files', () => {
+      const files = {
+        a: { preview: 'http://abc', isRestored: true }
+      }
+      const core = Object.assign(emitter(), {
+        getState () {
+          return { files }
+        },
+        getFile (id) {
+          return files[id]
+        }
+      })
+
+      const plugin = new ThumbnailGeneratorPlugin(core)
+      plugin.addToQueue = jest.fn()
+      plugin.install()
+
+      core.emit('restored')
+
+      expect(plugin.addToQueue).not.toHaveBeenCalled()
+    })
+  })
 })