123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- def new_upload(pub, file_name, meta_data=None):
- upload_date = dateutil.parser.parse(time.asctime()).isoformat()
- # set up meta.json
- meta_data_dict = json.loads(meta_data)
- meta_data_dict["upload_date"] = upload_date
- meta_data = json.dumps(meta_data_dict)
- # create ContentUpload
- upload = models.ContentUpload()
- upload.status = "Saving"
- upload.publisher = pub
- upload.meta = meta_data
- upload.username = meta_data_dict.get("username", "")
- upload.source = meta_data_dict.get("source") or meta_data_dict.get("url", "")
- upload.upload_date = upload_date
- if "upload_type" in meta_data_dict:
- upload.upload_type = meta_data_dict["upload_type"]
- elif pub.name == "BED" and "hotspot" in meta_data_dict:
- upload.upload_type = (
- publisher_types.HOTSPOT
- if meta_data_dict["hotspot"]
- else publisher_types.TARGET
- )
- else:
- upload.upload_type = pub.name
- upload.save()
- upload_dir = os.path.join("/results/uploads", pub.name, str(upload.pk))
- upload.file_path = os.path.join(upload_dir, file_name)
- upload.save()
- try:
- meta_path = os.path.join(upload_dir, "meta.json")
- # set both the user and group to read/write to allow the celery tasks to write to this directory
- original_umask = os.umask(0)
- os.makedirs(upload_dir, 0o0775)
- open(meta_path, "w").write(meta_data)
- except OSError as err:
- logger.exception("File error while saving new %s upload" % pub)
- upload.status = "Error: %s" % err
- upload.save()
- raise
- finally:
- os.umask(original_umask)
- return upload
|