Exemple #1
0
def upload_add(request):
    # add irods file into an existing resource
    res_id = request.POST['res_id']
    resource, _, _ = authorize(request, res_id, edit=True, full=True, superuser=True)
    res_files = request.FILES.getlist('files')
    extract_metadata = request.REQUEST.get('extract-metadata', 'No')
    extract_metadata = True if extract_metadata.lower() == 'yes' else False

    irods_fname = request.POST.get('irods_file_name', '')
    res_cls = resource.__class__
    file_types = res_cls.get_supported_upload_file_types()
    valid = False
    if file_types == ".*":
        valid = True
    else:
        ext = os.path.splitext(irods_fname)[1]
        if ext == file_types:
            valid = True
        else:
            for index in range(len(file_types)):
                file_type_str = file_types[index].strip()
                if file_type_str == ".*" or ext == file_type_str:
                    valid = True
                    break

    if not valid:
        request.session['file_type_error'] = "Invalid file type: {ext}".format(ext=ext)
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
    else:
        user = request.POST.get('irods-username')
        password = request.POST.get("irods-password")
        port = request.POST.get("irods-port")
        host = request.POST.get("irods-host")
        zone = request.POST.get("irods-zone")
        try:
            upload_from_irods(username=user, password=password, host=host, port=port,
                              zone=zone, irods_fname=irods_fname, res_files=res_files)
        except Exception as ex:
            request.session['file_validation_error'] = ex.message
            return HttpResponseRedirect(request.META['HTTP_REFERER'])

    try:
        utils.resource_file_add_pre_process(resource=resource, files=res_files, user=request.user,
                                            extract_metadata=extract_metadata)

    except hydroshare.utils.ResourceFileSizeException as ex:
        request.session['file_size_error'] = ex.message
        return HttpResponseRedirect(request.META['HTTP_REFERER'])

    except (hydroshare.utils.ResourceFileValidationException, Exception) as ex:
        request.session['file_validation_error'] = ex.message
        return HttpResponseRedirect(request.META['HTTP_REFERER'])

    try:
        hydroshare.utils.resource_file_add_process(resource=resource, files=res_files, user=request.user,
                                                   extract_metadata=extract_metadata)

    except (hydroshare.utils.ResourceFileValidationException, Exception) as ex:
        request.session['file_validation_error'] = ex.message

    return HttpResponseRedirect(request.META['HTTP_REFERER'])
Exemple #2
0
def upload_add(request):
    # add irods file into an existing resource
    res_id = request.POST.get('res_id', '')
    resource, _, _ = authorize(request, res_id, needed_permission=ACTION_TO_AUTHORIZE.EDIT_RESOURCE)
    res_files = request.FILES.getlist('files')
    extract_metadata = request.POST.get('extract-metadata', 'No')
    extract_metadata = True if extract_metadata.lower() == 'yes' else False
    irods_fnames = request.POST.get('upload', '')
    irods_fnames_list = irods_fnames.split(',')
    res_cls = resource.__class__

    # TODO: read resource type from resource, not from input file 
    valid, ext = check_upload_files(res_cls, irods_fnames_list)
    source_names = []
    if not valid:
        return JsonResponse({'error': "Invalid file type: {ext}".format(ext=ext)},
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        homepath = irods_fnames_list[0]
        # TODO: this should happen whether resource is federated or not
        irods_federated = utils.is_federated(homepath)
        if irods_federated:
            source_names = irods_fnames.split(',')
        else:
            user = request.POST.get('irods_username')
            password = request.POST.get("irods_password")
            port = request.POST.get("irods_port")
            host = request.POST.get("irods_host")
            zone = request.POST.get("irods_zone")
            try:
                upload_from_irods(username=user, password=password, host=host, port=port,
                                  zone=zone, irods_fnames=irods_fnames, res_files=res_files)
            except SessionException as ex:
                return JsonResponse(
                    {"error": ex.stderr}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )

    try:
        utils.resource_file_add_pre_process(resource=resource, files=res_files, user=request.user,
                                            extract_metadata=extract_metadata, 
                                            source_names=source_names, folder='')
    except hydroshare.utils.ResourceFileSizeException as ex:
        return JsonResponse({'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST)

    except (hydroshare.utils.ResourceFileValidationException, Exception) as ex:
        return JsonResponse({'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST)

    try:
        hydroshare.utils.resource_file_add_process(resource=resource, files=res_files, 
                                                   user=request.user,
                                                   extract_metadata=extract_metadata,
                                                   source_names=source_names, folder='')

    except (hydroshare.utils.ResourceFileValidationException, SessionException) as ex:
        if str(ex):
            return JsonResponse({'error': str(ex)},
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        elif ex.stderr:
            return JsonResponse({'error': ex.stderr},
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    return JsonResponse({}, status=status.HTTP_200_OK)
def upload_add(request):
    # add irods file into an existing resource
    res_id = request.POST['res_id']
    resource, _, _ = authorize(request, res_id, needed_permission=ACTION_TO_AUTHORIZE.EDIT_RESOURCE)
    res_files = request.FILES.getlist('files')
    extract_metadata = request.POST.get('extract-metadata', 'No')
    extract_metadata = True if extract_metadata.lower() == 'yes' else False
    irods_fnames = request.POST.get('irods_file_names', '')
    irods_fnames_list = string.split(irods_fnames, ',')
    res_cls = resource.__class__

    # TODO: read resource type from resource, not from input file 
    valid, ext = check_upload_files(res_cls, irods_fnames_list)
    source_names = []
    irods_federated = False
    if not valid:
        request.session['file_type_error'] = "Invalid file type: {ext}".format(ext=ext)
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
    else:
        homepath = irods_fnames_list[0]
        # TODO: this should happen whether resource is federated or not
        irods_federated = utils.is_federated(homepath)
        if irods_federated:
            source_names = irods_fnames.split(',')
        else:
            user = request.POST.get('irods-username')
            password = request.POST.get("irods-password")
            port = request.POST.get("irods-port")
            host = request.POST.get("irods-host")
            zone = request.POST.get("irods-zone")
            try:
                upload_from_irods(username=user, password=password, host=host, port=port,
                                  zone=zone, irods_fnames=irods_fnames, res_files=res_files)
            except SessionException as ex:
                request.session['validation_error'] = ex.stderr
                return HttpResponseRedirect(request.META['HTTP_REFERER'])

    try:
        utils.resource_file_add_pre_process(resource=resource, files=res_files, user=request.user,
                                            extract_metadata=extract_metadata, 
                                            source_names=source_names, folder=None)
    except hydroshare.utils.ResourceFileSizeException as ex:
        request.session['file_size_error'] = ex.message
        return HttpResponseRedirect(request.META['HTTP_REFERER'])

    except (hydroshare.utils.ResourceFileValidationException, Exception) as ex:
        request.session['validation_error'] = ex.message
        return HttpResponseRedirect(request.META['HTTP_REFERER'])

    try:
        hydroshare.utils.resource_file_add_process(resource=resource, files=res_files, 
                                                   user=request.user,
                                                   extract_metadata=extract_metadata,
                                                   source_names=source_names, folder=None)

    except (hydroshare.utils.ResourceFileValidationException, Exception) as ex:
        if ex.message:
            request.session['validation_error'] = ex.message
        elif ex.stderr:
            request.session['validation_error'] = ex.stderr
    except SessionException as ex:
        request.session['validation_error'] = ex.stderr

    request.session['resource-mode'] = 'edit'
    return HttpResponseRedirect(request.META['HTTP_REFERER'])