Beispiel #1
0
def add_file(target_dir, source):
    """
    Deposit file stream into a target directory.
    """

    # Link an existing file
    if isinstance(source, str) and os.path.exists(source):
        return link_file(source=source, target_dir=target_dir)

    # Write a stream to a new file
    if hasattr(source, 'read'):
        # Get the absolute path
        dest = os.path.abspath(target_dir)

        # Create the directory
        os.makedirs(dest, exist_ok=True)

        # Get the name
        fname = source.name

        path = os.path.abspath(os.path.join(dest, fname))
        # Write the stream into file.
        util.write_stream(stream=source, dest=path)

        return path

    return
Beispiel #2
0
def create_data(project, user=None, stream=None, path='', name='', text='', type='', uid=None):

    # We need absolute paths with no trailing slashes.
    path = os.path.abspath(path).rstrip("/") if path else ""

    # Create the data.
    dtype = type or "DATA"

    # The owner of the data will be the first admin user if not set otherwise.
    owner = user or models.User.objects.filter(is_staff=True).first()

    # Create the data object.
    data = Data.objects.create(name=name, owner=owner, state=Data.PENDING,
                               project=project, type=dtype, text=text, uid=uid)

    # Set the uid.
    uid = new_uid(obj=data, objtype=Data, default=uid)
    data.uid = uid

    # Write this stream into a path then link that into the data.
    if stream:
        name = name or stream.name
        fname = '_'.join(name.split())
        # Create path for the stream
        path = create_path(data=data, fname=fname)

        # Write stream into newly created path.
        util.write_stream(stream=stream, dest=path)

        # Mark incoming file as uploaded
        data.method = Data.UPLOAD

    # Link path to this data object.
    create_data_link(path=path, data=data)

    # Invalid paths and empty streams still create the data
    # but set the data state will be set to error.
    missing = not (path or stream or os.path.isdir(path) or os.path.isfile(path))

    # An invalid entry here.
    if path and missing:
        state = Data.ERROR
        logger.error(f"Invalid data path: {path}")
    else:
        state = Data.READY

    # Set updated attributes
    data.state = state
    data.name = name or os.path.basename(path) or 'Data'

    # Trigger another save to remake the toc file.
    data.save()

    # Set log for data creation.
    logger.info(f"Added data type={data.type} name={data.name} pk={data.pk}")

    return data
Beispiel #3
0
def data_api(request):
    """
    GET request: Returns data
    PUT request: Updates file in data with given file.
    """

    uid = request.GET.get('uid', request.POST.get('uid'))
    data = Data.objects.filter(uid=uid).first()

    # Get the source that will replace target
    source = request.FILES.get("data", "")

    # Target first file in data directory.
    target = data.get_files()[0]

    if not target:
        msg = f"File does not exist."
        return HttpResponse(content=msg, content_type="text/plain")

    # Write source into target
    if request.method == "POST":
        # Validate source and target files before upload.
        valid, msg = auth.validate_file(source=source)
        if not valid:
            return HttpResponse(content=msg, content_type="text/plain")

        # Write source to target file.
        target = util.write_stream(stream=source, dest=target)

    # Return file contents in payload
    payload = open(target, 'r').read()

    return HttpResponse(content=payload, content_type="text/plain")