     new Uint8Array fileContent.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()
+      uploadMarkComplete
+        errored: true
+        status: error.toString()
       return
 
-    ImportingFile.documents.update file._id,
-      $set:
-        finished: true
-        publicationId: publicationId
+    uploadMarkComplete
+      finished: true
+      publicationId: publicationId
 
 uploadFile = (file, publicationId) ->
+  ImportingFile.documents.update file._id,
+    $set:
+      status: "Uploading file"
   meteorFile = new MeteorFile file,
     collection: ImportingFile.Meta.collection
 

DRAGGING_OVER_DOM = false
fileBuffer = []

# observing files with computed checksums
checksummed = ImportingFile.documents.find
  sha256:
    $exists: true
checksummed.observe
  added: (document) ->
    id = document._id
    file = fileBuffer[id].file
    fileContent = fileBuffer[id].fileContent

    console.log "Creating publication for file " + document.name
    Meteor.call 'create-publication', document.name, document.sha256, (error, result) ->
      if error
        ImportingFile.documents.update id,
          $set:
            errored: true
            status: error.toString()
        return

      if result.already
        ImportingFile.documents.update id,
          $set:
            finished: true
            status: "File already imported"
            publicationId: result.publicationId
        return

      if result.verify
        console.log "Verifying file " + document.name
        verifyFile file, fileContent, result.publicationId, result.samples
      else
        console.log "Uploading file " + document.name
        uploadFile file, result.publicationId
     
      # remove file from buffer 
      delete fileBuffer[id]

# observing files that are ready for checksum computation
preprocessed = ImportingFile.documents.find
  preprocessed:
    $exists: true
preprocessed.observe
  added: (document) ->
    id = document._id
    fileContent = fileBuffer[id].fileContent

    console.log "Computing checksum for " + document.name
    ImportingFile.documents.update id,
      $set:
        status: "Computing checksum"

    hash = new Crypto.SHA256
      onProgress: (progress) ->
        #TODO: update progressbar
    hash.update fileContent, (error, result) ->
      #TODO: handle errors
      if error
        console.log "Import error: " + error.message
    hash.finalize (error, result) ->
      #TODO: handle errors
      if error
        console.log "Import error: " + error.message

      alreadyImporting = ImportingFile.documents.findOne
        sha256: result

      if alreadyImporting
        ImportingFile.documents.update id,
          $set:
            finished: true
            status: "File is already importing"
            # publicationId might not yet be available, but let's try
            publicationId: alreadyImporting.publicationId
        return

      console.log "Setting sha256 value"
      # so that observer can do it's work
      ImportingFile.documents.update id,
        $set:
          sha256: result

verifyFile = (file, fileContent, publicationId, samples) ->
  ImportingFile.documents.update file._id,

    uploadProgress: 0
    finished: false
    errored: false
  ,
    # We are using callback to make sure ImportingFiles really has the file now
    (error, id) ->
