123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- def write_plupload(request, pub_name):
- """file upload for plupload"""
- logger.info("Starting write plupload")
- pub = models.Publisher.objects.get(name=pub_name)
- meta = request.POST.get("meta", "{}")
- logger.debug("%s" % meta)
- if request.method == "POST":
- name = request.REQUEST.get("name", "")
- uploaded_file = request.FILES["file"]
- if not name:
- name = uploaded_file.name
- logger.debug("plupload name = '%s'" % name)
- try:
- labelsContent = labels.TargetRegionsContent # default case
- if RepresentsJSON(meta):
- metaJson = json.loads(meta)
- labelsContent = (
- labels.HotspotsContent
- if metaJson and metaJson["hotspot"]
- else labels.TargetRegionsContent
- )
- file_name = validate_plupload(request, pub_name, name, labelsContent)
- except Exception as err:
- return HttpResponseBadRequest(unicode(err))
- upload_dir = "/results/referenceLibrary/temp"
- if not os.path.exists(upload_dir):
- return render_to_json({"error": validation.missing_error(upload_dir)})
- dest_path = os.path.join(upload_dir, file_name)
- logger.debug("plupload destination = '%s'" % dest_path)
- chunk = request.REQUEST.get("chunk", "0")
- chunks = request.REQUEST.get("chunks", "0")
- logger.debug(
- "plupload chunk %s %s of %s" % (str(type(chunk)), str(chunk), str(chunks))
- )
- debug = [chunk, chunks]
- with open(dest_path, ("wb" if chunk == "0" else "ab")) as f:
- for content in uploaded_file.chunks():
- logger.debug("content chunk = '%d'" % len(content))
- f.write(content)
- my_contentupload_id = None
- if int(chunk) + 1 >= int(chunks):
- try:
- meta = json.loads(meta)
- meta["username"] = request.user.username
- upload = move_upload(pub, dest_path, file_name, json.dumps(meta))
- async_upload = run_pub_scripts.delay(pub, upload)
- my_contentupload_id = upload.id
- except Exception as err:
- logger.exception(
- "There was a problem during upload of a file for a publisher."
- )
- else:
- logger.info("Successfully pluploaded %s" % name)
- logger.debug("plupload done")
- return render_to_json(
- {"chunk posted": debug, "contentupload_id": my_contentupload_id}
- )
- else:
- return render_to_json(
- {
- "method": i18n_errors.fatal_unsupported_http_method_expected(
- request.method, "POST"
- )
- }
- )
|