           imported: true
           publicationId: publicationId
 
-testPDF = (file, fileContent, callback) ->
-  PDFJS.getDocument(data: fileContent, password: '').then callback, (message, exception) ->
+# process next element from import queue
+Deps.autorun ->
+  # We exclude status and progress fields so that function
+  # doesn't get recalled on every status or progress change
+  document = ImportingFile.documents.findOne
+    finished: false
+    errored: false
+    sha256:
+      $exists: true
+  ,
+    fields:
+      status: 0
+      uploadProgress: 0
+      readProgress: 0
+  return unless document
+
+  console.log "Autorun called for import queue " + document.name
+
+  Meteor.call 'create-publication', document.name, document.sha256, (error, result) ->
+    if error
+      ImportingFile.documents.update document._id,
+        $set:
+          errored: true
+          status: error.toString()
+      return
+
+    if result.already
+      ImportingFile.documents.update document._id,
+        $set:
+          finished: true
+          status: "File already imported"
+          publicationId: result.publicationId
+      return
+
+    if result.verify
+      verifyFile importingFilesArrayBuffer[document._id], result.publicationId, result.samples
+    else
+      uploadFile importingFilesArrayBuffer[document._id], result.publicationId
+
+
+computeChecksum = (file, callback) ->
+  hash = new Crypto.SHA256
+    onProgress: (progress) ->
+      #TODO: update progressbar
+  hash.update file.content, (error, result) ->
+    if error
+      ImportingFile.documents.update file._id,
+        $set:
+          errored: true
+          status: error.toString()
+    return
+  hash.finalize callback
+
+testPDF = (file, callback) ->
+  PDFJS.getDocument(data: file.content, password: '').then callback, (message, exception) ->
     ImportingFile.documents.update file._id,
       $set:
         errored: true
         status: "Invalid PDF file"
 
-importFile = (file) ->
+# process next element from preprocess queue
+Deps.autorun ->
+  # we exclude status field so that it doesn't get recalled on status change
+  document = ImportingFile.documents.findOne
+    sha256:
+      $exists: false
+    buffered: true
+  ,
+    fields:
+      status: 0
+  return unless document
+
+  ImportingFile.documents.update document._id,
+    $set:
+      status: "Preprocessing"
+
+  console.log "Called autorun for preprocessing queue " + document.name
+
   reader = new FileReader()
   reader.onload = ->
-    fileContent = @result
-    testPDF file, fileContent, ->
-      fileBuffer[file._id] =
-        file: file
-        fileContent: fileContent
-
-      ImportingFile.documents.update file._id,
-        $set:
-          status: "In queue for checksum computation"
-          preprocessed: true
-
+    importingFilesArrayBuffer[document._id].content = @result
+    console.log "Testing PDF with PDFJS"
+    testPDF importingFilesArrayBuffer[document._id], ->
+      console.log "Computing checksum"
+      computeChecksum importingFilesArrayBuffer[document._id], (error, result) ->
+        if error
+          ImportingFile.documents.update document._id,
+            $set:
+              errored: true
+              status: error.toString()
+          return
+
+        ImportingFile.documents.update document._id,
+          $set:
+            sha256: result
+            status: "In import queue"
+
+  #TODO: read file in chunks
+  reader.readAsArrayBuffer importingFilesArrayBuffer[document._id]
+
+
+importFile = (file) ->
   ImportingFile.documents.insert
     name: file.name
-    status: "Preprocessing file"
+    status: "In preprocessing queue"
     readProgress: 0
     uploadProgress: 0
     finished: false

        status: "File imported"

uploadFile = (file, publicationId) ->
  ImportingFile.documents.update file._id,
    $set:
      status: "Uploading file"
  meteorFile = new MeteorFile file,
    collection: ImportingFile.Meta.collection



UPLOAD_CHUNK_SIZE = 128 * 1024 # bytes
DRAGGING_OVER_DOM = false
importingFilesArrayBuffer = []

# observes files that are processed and removes them from buffer
processedFiles = ImportingFile.documents.find()
processedFiles.observe
  removed: (document) ->
    console.log "Removing document from buffer " + document.name
    delete importingFilesArrayBuffer[document._id]


verifyFile = (file, publicationId, samples) ->
  ImportingFile.documents.update file._id,
    $set:
      status: "Verifying file"

  samplesData = _.map samples, (sample) ->
    new Uint8Array file.content.slice sample.offset, sample.offset + sample.size
  Meteor.call 'verify-publication', publicationId, samplesData, (error) ->
    if error
      ImportingFile.documents.update file._id,
        $set:
          errored: true
          status: error.toString()
      return

    ImportingFile.documents.update file._id,
      $set:
        finished: true
        imported: true
