 # Local (client-only) document of importing files
 class ImportingFile extends Document
   # name: user's file name
-  # readProgress: progress of reading from file, in %
+  # preprocessingProgress: progress of reading from file, in %
   # uploadProgress: progress of uploading file, in %
   # status: current status or error message
   # finished: true when importing has finished

            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: "In preprocessing queue"
    readProgress: 0
    uploadProgress: 0
    finished: false
    errored: false
    canceled: false
    imported: false
  ,
    # We are using callback to make sure ImportingFiles really has the file now
    (error, id) ->
      return Notify.meteorError error, true if error

      # So that meteor-file knows what to update
      file._id = id
      importingFilesArrayBuffer[file._id] = file

      # this will indicate that the file is ready for preprocessing
      ImportingFile.documents.update id,
        $set:
          buffered: true

hideOverlay = ->
  allCount = ImportingFile.documents.find().count()

        errored: true
        status: "Invalid PDF file"

# Process next element from preprocessing queue
Deps.autorun ->
  document = ImportingFile.documents.findOne
    sha256:
      $exists: false
  ,
    fields:
      _id: 1
  return unless document

  ImportingFile.documents.update document._id,
    $set:
      status: "Preprocessing"

  reader = new FileReader()
  reader.onload = ->
    importingFiles[document._id].content = @result
    testPDF importingFiles[document._id], ->
      computeChecksum importingFiles[document._id], (error, result) ->
        if error
          ImportingFile.documents.update document._id,
            $set:
