Beispiel #1
0
def image_upload(request):
    # Used for content nodes
    if request.method != 'POST':
        return HttpResponseBadRequest("Only POST requests are allowed on this endpoint.")

    fobj = request.FILES.values()[0]
    name, ext = os.path.splitext(fobj._name)  # gets file extension without leading period
    checksum = get_hash(DjFile(fobj))
    request.user.check_space(fobj._size, checksum)

    file_object = File(
        contentnode_id=request.META.get('HTTP_NODE'),
        original_filename=name,
        preset_id=request.META.get('HTTP_PRESET'),
        file_on_disk=DjFile(request.FILES.values()[0]),
        file_format_id=ext[1:].lower(),
        uploaded_by=request.user
    )
    file_object.save()
    return HttpResponse(json.dumps({
        "success": True,
        "file": JSONRenderer().render(FileSerializer(file_object).data),
        "path": generate_storage_url(str(file_object)),
        "encoding": get_thumbnail_encoding(str(file_object)),
    }))
Beispiel #2
0
def map_files_to_assessment_item(user, assessment_item, data):
    """
    Generate files referenced in given assesment item (a.k.a. question).
    """
    if settings.DEBUG:
        # assert that our parameters match expected values
        assert isinstance(user, User)
        assert isinstance(assessment_item, AssessmentItem)
        assert isinstance(data, list)

    # filter out file that are empty
    valid_data = filter_out_nones(data)

    for file_data in valid_data:
        filename = file_data["filename"]
        checksum, ext = filename.split(".")

        file_path = generate_object_storage_name(checksum, filename)
        storage = default_storage
        if not storage.exists(file_path):
            raise IOError('{} not found'.format(file_path))

        resource_obj = File(
            checksum=checksum,
            assessment_item=assessment_item,
            file_format_id=ext,
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            preset_id=file_data[
                "preset"],  # assessment_item-files always have a preset
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()
Beispiel #3
0
def map_files_to_slideshow_slide_item(user, node, slides, files):
    """
    Generate files referenced in given slideshow slide
    """
    for file_data in files:
        filename = file_data["filename"]
        checksum, ext = filename.split(".")

        matching_slide = next(
            (slide
             for slide in slides if slide.metadata["checksum"] == checksum),
            None)

        if not matching_slide:
            # TODO(Jacob) Determine proper error type... raise it.
            print("NO MATCH")

        file_path = generate_object_storage_name(checksum, filename)
        storage = default_storage

        if not storage.exists(file_path):
            raise IOError('{} not found'.format(file_path))

        file_obj = File(slideshow_slide=matching_slide,
                        checksum=checksum,
                        file_format_id=ext,
                        original_filename=file_data.get("original_filename")
                        or "file",
                        source_url=file_data.get("source_url"),
                        file_size=file_data["size"],
                        preset_id=file_data["preset"],
                        uploaded_by=user)

        file_obj.file_on_disk.name = file_path
        file_obj.save()
Beispiel #4
0
def create_file(display_name, preset_id, ext, user=None):
    with tempfile.NamedTemporaryFile(suffix=".{}".format(ext),
                                     mode='w+t',
                                     delete=False) as f:
        f.write(":)")
        f.flush()
        size = f.tell()
        filename = write_file_to_storage(f, name=f.name)
        checksum, _ext = os.path.splitext(filename)

        f.seek(0)

        file_object = File(
            file_size=size,
            file_on_disk=DjFile(f),
            checksum=checksum,
            file_format_id=ext,
            original_filename=display_name,
            preset_id=preset_id,
            uploaded_by=user,
        )
        file_object.save()
        f.close()

        os.unlink(f.name)

        return file_object
Beispiel #5
0
def create_file(display_name, preset_id, ext, user=None):
    with tempfile.NamedTemporaryFile(suffix=".{}".format(ext), mode='w+t', delete=False) as f:
        f.write(":)")
        f.flush()
        size = f.tell()
        filename = write_file_to_storage(f, name=f.name)
        checksum, _ext = os.path.splitext(filename)

        f.seek(0)

        file_object = File(
            file_size=size,
            file_on_disk=DjFile(f),
            checksum=checksum,
            file_format_id=ext,
            original_filename=display_name,
            preset_id=preset_id,
            uploaded_by=user,
            language_id="mul" if FormatPreset.objects.filter(id=preset_id, multi_language=True).exists() else None,
        )
        file_object.save()
        f.close()

        os.unlink(f.name)

        return file_object
Beispiel #6
0
def image_upload(request):
    if request.method != 'POST':
        return HttpResponseBadRequest(
            "Only POST requests are allowed on this endpoint.")

    fobj = request.FILES.values()[0]
    name, ext = os.path.splitext(
        fobj._name)  # gets file extension without leading period
    checksum = get_hash(DjFile(fobj))
    request.user.check_space(fobj._size, checksum)

    file_object = File(contentnode_id=request.META.get('HTTP_NODE'),
                       original_filename=name,
                       preset_id=request.META.get('HTTP_PRESET'),
                       file_on_disk=DjFile(request.FILES.values()[0]),
                       file_format_id=ext[1:].lower(),
                       uploaded_by=request.user)
    file_object.save()
    return HttpResponse(
        json.dumps({
            "success":
            True,
            "file":
            JSONRenderer().render(FileSerializer(file_object).data),
            "path":
            generate_storage_url(str(file_object)),
        }))
Beispiel #7
0
def map_files_to_assessment_item(user, assessment_item, data):
    """
    Generate files referenced in given assesment item (a.k.a. question).
    """
    if settings.DEBUG:
        # assert that our parameters match expected values
        assert isinstance(user, User)
        assert isinstance(assessment_item, AssessmentItem)
        assert isinstance(data, list)

    # filter out file that are empty
    valid_data = filter_out_nones(data)

    for file_data in valid_data:
        filename = file_data["filename"]
        checksum, ext = filename.split(".")

        file_path = generate_object_storage_name(checksum, filename)
        storage = default_storage
        if not storage.exists(file_path):
            raise IOError('{} not found'.format(file_path))

        resource_obj = File(
            checksum=checksum,
            assessment_item=assessment_item,
            file_format_id=ext,
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            preset_id=file_data["preset"],   # assessment_item-files always have a preset
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()
def map_files_to_node(node, data):
    """ Generate files that reference the content node """
    for file_data in data:
        file_hash = file_data['filename'].split(".")

        # Determine a preset if none is given
        kind_preset = None
        if file_data['preset'] is None:
            kind_preset = FormatPreset.objects.filter(kind=node.kind, allowed_formats__extension__contains=file_hash[1], display=True).first()
        else:
            kind_preset = FormatPreset.objects.get(id=file_data['preset'])

        language = None
        if file_data.get('language'):
            language = Language.objects.get(pk=file_data['language'])

        file_path=generate_file_on_disk_name(file_hash[0], file_data['filename'])
        if not os.path.isfile(file_path):
            raise IOError('{} not found'.format(file_path))

        file_obj = File(
            checksum=file_hash[0],
            contentnode=node,
            file_format_id=file_hash[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size = file_data['size'],
            file_on_disk=DjFile(open(file_path, 'rb')),
            preset=kind_preset,
            language=language,
        )
        file_obj.save()
Beispiel #9
0
def file_upload(request):
    if request.method != 'POST':
        return HttpResponseBadRequest("Only POST requests are allowed on this endpoint.")

    # Implement logic for switching out files without saving it yet
    filename, ext = os.path.splitext(request.FILES.values()[0]._name)
    size = request.FILES.values()[0]._size
    contentfile = DjFile(request.FILES.values()[0])
    checksum = get_hash(contentfile)
    request.user.check_space(size, checksum)

    file_object = File(
        file_size=size,
        file_on_disk=contentfile,
        checksum=checksum,
        file_format_id=ext[1:].lower(),
        original_filename=request.FILES.values()[0]._name,
        preset_id=request.META.get('HTTP_PRESET'),
        language_id=request.META.get('HTTP_LANGUAGE'),
        uploaded_by=request.user,
    )
    file_object.save()

    return HttpResponse(json.dumps({
        "success": True,
        "filename": str(file_object),
        "file": JSONRenderer().render(FileSerializer(file_object).data)
    }))
Beispiel #10
0
def file_upload(request):
    if request.method == 'POST':
        # Implement logic for switching out files without saving it yet
        filename, ext = os.path.splitext(request.FILES.values()[0]._name)
        size = request.FILES.values()[0]._size
        contentfile = DjFile(request.FILES.values()[0])
        checksum = get_hash(contentfile)
        request.user.check_space(size, checksum)

        file_object = File(
            file_size=size,
            file_on_disk=contentfile,
            checksum=checksum,
            file_format_id=ext[1:].lower(),
            original_filename=request.FILES.values()[0]._name,
            preset_id=request.META.get('HTTP_PRESET'),
            language_id=request.META.get('HTTP_LANGUAGE'),
            uploaded_by=request.user,
        )
        file_object.save()

        return HttpResponse(
            json.dumps({
                "success":
                True,
                "filename":
                str(file_object),
                "file":
                JSONRenderer().render(FileSerializer(file_object).data)
            }))
Beispiel #11
0
def create_file(display_name, preset_id, ext, user=None):
    with tempfile.NamedTemporaryFile(suffix=".{}".format(ext),
                                     mode="wb+",
                                     delete=False) as f:
        f.write(b":)")
        f.flush()
        size = f.tell()
        filename = write_file_to_storage(f, name=f.name)
        checksum, _ext = os.path.splitext(filename)

        f.seek(0)

        file_object = File(
            file_size=size,
            file_on_disk=DjFile(f),
            checksum=checksum,
            file_format_id=ext,
            original_filename=display_name,
            preset_id=preset_id,
            uploaded_by=user,
            language_id="mul" if FormatPreset.objects.filter(
                id=preset_id, multi_language=True).exists() else None,
        )
        file_object.save()
        f.close()

        os.unlink(f.name)

        return file_object
Beispiel #12
0
def exercise_image_upload(request):
    if request.method != 'POST':
        return HttpResponseBadRequest(
            "Only POST requests are allowed on this endpoint.")

    fobj = request.FILES.values()[0]
    name, ext = os.path.splitext(fobj._name)
    checksum = get_hash(DjFile(fobj))
    file_object = File(
        preset_id=format_presets.EXERCISE_IMAGE,
        file_on_disk=DjFile(request.FILES.values()[0]),
        file_format_id=ext[1:].lower(),
    )
    file_object.save()
    return HttpResponse(
        json.dumps({
            "success":
            True,
            "formatted_filename":
            exercises.CONTENT_STORAGE_FORMAT.format(str(file_object)),
            "file_id":
            file_object.pk,
            "path":
            generate_storage_url(str(file_object)),
        }))
Beispiel #13
0
def map_files_to_node(user, node, data):
    """
    Generate files that reference the content node.
    """
    if settings.DEBUG:
        # assert that our parameters match expected values
        assert isinstance(user, User)
        assert isinstance(node, ContentNode)
        assert isinstance(data, list)

    # filter out file that are empty
    valid_data = filter_out_nones(data)

    for file_data in valid_data:
        filename = file_data["filename"]
        checksum, ext1 = os.path.splitext(filename)
        ext = ext1.lstrip(".")

        # Determine a preset if none is given
        kind_preset = FormatPreset.get_preset(file_data["preset"]) or FormatPreset.guess_format_preset(filename)

        file_path = generate_object_storage_name(checksum, filename)
        storage = default_storage

        if not storage.exists(file_path):
            raise IOError('{} not found'.format(file_path))

        try:
            if file_data.get('language'):
                # TODO: Remove DB call per file?
                file_data['language'] = Language.objects.get(pk=file_data['language'])
        except ObjectDoesNotExist:
            invalid_lang = file_data.get('language')
            logging.warning("file_data with language {} does not exist.".format(invalid_lang))
            return ValidationError("file_data given was invalid; expected string, got {}".format(invalid_lang))

        resource_obj = File(
            checksum=checksum,
            contentnode=node,
            file_format_id=ext,
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            preset=kind_preset,
            language_id=file_data.get('language'),
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()

        # Handle thumbnail
        if resource_obj.preset and resource_obj.preset.thumbnail:
            node.thumbnail_encoding = json.dumps({
                'base64': get_thumbnail_encoding(str(resource_obj)),
                'points': [],
                'zoom': 0
            })
            node.save()
Beispiel #14
0
def map_files_to_node(user, node, data):
    """
    Generate files that reference the content node.
    """
    if settings.DEBUG:
        # assert that our parameters match expected values
        assert isinstance(user, User)
        assert isinstance(node, ContentNode)
        assert isinstance(data, list)

    # filter out file that are empty
    valid_data = filter_out_nones(data)

    for file_data in valid_data:
        filename = file_data["filename"]
        checksum, ext1 = os.path.splitext(filename)
        ext = ext1.lstrip(".")

        # Determine a preset if none is given
        kind_preset = FormatPreset.get_preset(file_data["preset"]) or FormatPreset.guess_format_preset(filename)

        file_path = generate_object_storage_name(checksum, filename)
        storage = default_storage

        if not storage.exists(file_path):
            raise IOError('{} not found'.format(file_path))

        try:
            if file_data.get('language'):
                # TODO: Remove DB call per file?
                file_data['language'] = Language.objects.get(pk=file_data['language'])
        except ObjectDoesNotExist:
            invalid_lang = file_data.get('language')
            logging.warning("file_data with language {} does not exist.".format(invalid_lang))
            return ValidationError("file_data given was invalid; expected string, got {}".format(invalid_lang))

        resource_obj = File(
            checksum=checksum,
            contentnode=node,
            file_format_id=ext,
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            preset=kind_preset,
            language_id=file_data.get('language'),
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()

        # Handle thumbnail
        if resource_obj.preset and resource_obj.preset.thumbnail:
            node.thumbnail_encoding = json.dumps({
                'base64': get_thumbnail_encoding(str(resource_obj)),
                'points': [],
                'zoom': 0
            })
            node.save()
Beispiel #15
0
def image_upload(request):
    if request.method == 'POST':
        name, ext = os.path.splitext(request.FILES.values()[0]._name) # gets file extension without leading period
        file_object = File(contentnode_id=request.META.get('HTTP_NODE'),original_filename=name, preset_id=request.META.get('HTTP_PRESET'), file_on_disk=DjFile(request.FILES.values()[0]), file_format_id=ext[1:])
        file_object.save()
        return HttpResponse(json.dumps({
            "success": True,
            "file": JSONRenderer().render(FileSerializer(file_object).data),
            "path": generate_storage_url(str(file_object)),
        }))
Beispiel #16
0
def thumbnail_upload(request):
    if request.method == 'POST':
        filename = request.FILES.values()[0]._name
        ext = os.path.splitext(filename)[1].split(".")[-1] # gets file extension without leading period
        file_object = File(file_on_disk=request.FILES.values()[0], file_format_id=ext, preset_id=format_presets.CHANNEL_THUMBNAIL)
        file_object.save()
        return HttpResponse(json.dumps({
            "success": True,
            "filename": "{0}.{ext}".format(file_object.checksum, ext=ext),
        }))
Beispiel #17
0
def file_create(request):
    if request.method == 'POST':
        original_filename, ext = os.path.splitext(
            request.FILES.values()[0]._name)
        size = request.FILES.values()[0]._size
        presets = FormatPreset.objects.filter(
            allowed_formats__extension__contains=ext[1:].lower())
        kind = presets.first().kind
        preferences = json.loads(request.user.preferences)
        author = preferences.get('author') or ""
        license = License.objects.filter(
            license_name=preferences.get('license')).first(
            )  # Use filter/first in case preference hasn't been set
        license_id = license.pk if license else settings.DEFAULT_LICENSE
        new_node = ContentNode(
            title=original_filename,
            kind=kind,
            license_id=license_id,
            author=author,
            copyright_holder=preferences.get('copyright_holder'))
        if license.license_name == licenses.SPECIAL_PERMISSIONS:
            new_node.license_description = preferences.get(
                'license_description')
        new_node.save()
        file_object = File(file_on_disk=DjFile(request.FILES.values()[0]),
                           file_format_id=ext[1:].lower(),
                           original_filename=request.FILES.values()[0]._name,
                           contentnode=new_node,
                           file_size=size)
        file_object.save()
        if kind.pk == content_kinds.VIDEO:
            file_object.preset_id = guess_video_preset_by_resolution(
                str(file_object.file_on_disk))
        elif presets.filter(supplementary=False).count() == 1:
            file_object.preset = presets.filter(supplementary=False).first()

        file_object.save()

        try:
            if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \
                or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \
                or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \
                or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT:
                generate_thumbnail_from_node(new_node, set_node=True)
        except Exception:
            pass

        return HttpResponse(
            json.dumps({
                "success":
                True,
                "node":
                JSONRenderer().render(
                    ContentNodeEditSerializer(new_node).data)
            }))
Beispiel #18
0
def exercise_image_upload(request):

    if request.method == 'POST':
        node = ContentNode.objects.get(id=request.META.get('HTTP_NODE'))
        ext = os.path.splitext(request.FILES.values()[0]._name)[1].split(".")[-1] # gets file extension without leading period
        file_object = File(file_on_disk=request.FILES.values()[0], file_format=FileFormat.objects.get(extension=ext), contentnode=node)
        file_object.save()
        return HttpResponse(json.dumps({
            "success": True,
            "filename": file_object.file_on_disk.url,
        }))
Beispiel #19
0
def exercise_image_upload(request):
    if request.method == 'POST':
        ext = os.path.splitext(request.FILES.values()[0]._name)[1][1:] # gets file extension without leading period
        file_object = File(preset_id=format_presets.EXERCISE_IMAGE, file_on_disk=DjFile(request.FILES.values()[0]), file_format_id=ext)
        file_object.save()
        return HttpResponse(json.dumps({
            "success": True,
            "formatted_filename": exercises.CONTENT_STORAGE_FORMAT.format(str(file_object)),
            "file_id": file_object.pk,
            "path": generate_storage_url(str(file_object)),
        }))
Beispiel #20
0
def map_files_to_node(user, node, data):
    """ Generate files that reference the content node """

    # filter for file data that's not empty;
    valid_data = (d for d in data if d)

    for file_data in valid_data:
        file_name_parts = file_data['filename'].split(".")

        # Determine a preset if none is given
        kind_preset = None
        if file_data['preset'] is None:
            kind_preset = FormatPreset.objects.filter(
                kind=node.kind,
                allowed_formats__extension__contains=file_name_parts[1],
                display=True).first()
        else:
            kind_preset = FormatPreset.objects.get(id=file_data['preset'])

        file_path = generate_object_storage_name(file_name_parts[0],
                                                 file_data['filename'])
        storage = default_storage
        if not storage.exists(file_path):
            return IOError('{} not found'.format(file_path))

        try:
            if file_data.get('language'):
                # TODO: Remove DB call per file?
                file_data['language'] = Language.objects.get(
                    pk=file_data['language'])
        except ObjectDoesNotExist as e:
            invalid_lang = file_data.get('language')
            logging.warning(
                "file_data with language {} does not exist.".format(
                    invalid_lang))
            return ValidationError(
                "file_data given was invalid; expected string, got {}".format(
                    invalid_lang))

        resource_obj = File(
            checksum=file_name_parts[0],
            contentnode=node,
            file_format_id=file_name_parts[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            file_on_disk=DjFile(storage.open(file_path, 'rb')),
            preset=kind_preset,
            language_id=file_data.get('language'),
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()
Beispiel #21
0
def create_file_from_contents(contents, ext=None, node=None, preset_id=None, uploaded_by=None):
    checksum, _, path = write_raw_content_to_storage(contents, ext=ext)

    result = File(
        file_format_id=ext,
        file_size=default_storage.size(path),
        checksum=checksum,
        preset_id=preset_id,
        contentnode=node,
        uploaded_by=uploaded_by
    )
    result.file_on_disk.name = path
    result.save()
    return result
Beispiel #22
0
def file_upload(request):
    if request.method == 'POST':
        preset = FormatPreset.objects.get(id=request.META.get('HTTP_PRESET'))
        #Implement logic for switching out files without saving it yet
        ext = os.path.splitext(request.FILES.values()[0]._name)[1].split(".")[-1]
        original_filename = request.FILES.values()[0]._name
        size = request.FILES.values()[0]._size
        file_object = File(file_size=size, file_on_disk=request.FILES.values()[0], file_format=FileFormat.objects.get(extension=ext), original_filename = original_filename, preset=preset)
        file_object.save()
        return HttpResponse(json.dumps({
            "success": True,
            "filename": str(file_object),
            "object_id": file_object.pk
        }))
Beispiel #23
0
def create_file_from_contents(contents, ext=None, node=None, preset_id=None, uploaded_by=None):
    checksum, _, path = write_raw_content_to_storage(contents, ext=ext)

    result = File(
        file_format_id=ext,
        file_size=default_storage.size(path),
        checksum=checksum,
        preset_id=preset_id,
        contentnode=node,
        uploaded_by=uploaded_by
    )
    result.file_on_disk.name = path
    result.save()
    return result
Beispiel #24
0
def file_create(request):
    if request.method == 'POST':
        ext = os.path.splitext(request.FILES.values()[0]._name)[1].split(".")[-1]
        size = request.FILES.values()[0]._size
        kind = FormatPreset.objects.filter(allowed_formats__extension__contains=ext).first().kind
        original_filename = request.FILES.values()[0]._name
        new_node = ContentNode(title=original_filename.split(".")[0], kind=kind, license_id=settings.DEFAULT_LICENSE, author=request.user.get_full_name())
        new_node.save()
        file_object = File(file_on_disk=request.FILES.values()[0], file_format=FileFormat.objects.get(extension=ext), original_filename = original_filename, contentnode=new_node, file_size=size)
        file_object.save()

        return HttpResponse(json.dumps({
            "success": True,
            "object_id": new_node.pk
        }))
Beispiel #25
0
    def upload_url(self, request):
        try:
            size = request.data["size"]
            checksum = request.data["checksum"]
            filename = request.data["name"]
            file_format = request.data["file_format"]
            preset = request.data["preset"]
        except KeyError:
            raise HttpResponseBadRequest(
                reason=
                "Must specify: size, checksum, name, file_format, and preset")

        try:
            request.user.check_space(float(size), checksum)

        except PermissionDenied as e:
            return HttpResponseBadRequest(reason=str(e), status=418)

        might_skip = File.objects.filter(checksum=checksum).exists()

        filepath = generate_object_storage_name(checksum, filename)
        checksum_base64 = codecs.encode(codecs.decode(checksum, "hex"),
                                        "base64").decode()
        retval = get_presigned_upload_url(filepath,
                                          checksum_base64,
                                          600,
                                          content_length=size)

        file = File(
            file_size=size,
            checksum=checksum,
            original_filename=filename,
            file_on_disk=filepath,
            file_format_id=file_format,
            preset_id=preset,
            uploaded_by=request.user,
        )

        # Avoid using our file_on_disk attribute for checks
        file.save(set_by_file_on_disk=False)

        retval.update({
            "might_skip": might_skip,
            "file": self.serialize_object(id=file.id)
        })

        return Response(retval)
Beispiel #26
0
def compress_video_wrapper(file_object):
    with tempfile.NamedTemporaryFile(suffix=".{}".format(file_formats.MP4)) as tempf:
        tempf.close()
        compress_video(str(file_object.file_on_disk), tempf.name, overwrite=True)
        filename = write_file_to_storage(open(tempf.name, 'rb'), name=tempf.name)
        checksum, ext = os.path.splitext(filename)
        file_location = generate_file_on_disk_name(checksum, filename)
        low_res_object = File(
            file_on_disk=DjFile(open(file_location, 'rb')),
            file_format_id=file_formats.MP4,
            original_filename=file_object.original_filename,
            contentnode=file_object.contentnode,
            file_size=os.path.getsize(file_location),
            preset_id=format_presets.VIDEO_LOW_RES,
        )
        low_res_object.save()
        return low_res_object
Beispiel #27
0
def compress_video_wrapper(file_object):
    with tempfile.NamedTemporaryFile(suffix=".{}".format(file_formats.MP4)) as tempf:
        tempf.close()
        compress_video(str(file_object.file_on_disk), tempf.name, overwrite=True)
        filename = write_file_to_storage(open(tempf.name, 'rb'), name=tempf.name)
        checksum, ext = os.path.splitext(filename)
        file_location = generate_file_on_disk_name(checksum, filename)
        low_res_object = File(
            file_on_disk=DjFile(open(file_location, 'rb')),
            file_format_id=file_formats.MP4,
            original_filename = file_object.original_filename,
            contentnode=file_object.contentnode,
            file_size=os.path.getsize(file_location),
            preset_id=format_presets.VIDEO_LOW_RES,
        )
        low_res_object.save()
        return low_res_object
def map_files_to_assessment_item(question, data):
    """ Generate files that reference the content node's assessment items """
    for file_data in data:
        file_hash = file_data['filename'].split(".")
        kind_preset = FormatPreset.objects.get(id=file_data['preset'])

        file_obj = File(
            checksum=file_hash[0],
            assessment_item=question,
            file_format_id=file_hash[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size = file_data['size'],
            file_on_disk=DjFile(open(generate_file_on_disk_name(file_hash[0], file_data['filename']), 'rb')),
            preset=kind_preset,
        )
        file_obj.save()
def map_files_to_assessment_item(question, data):
    """ Generate files that reference the content node's assessment items """
    for file_data in data:
        file_hash = file_data['filename'].split(".")
        file_path = generate_file_on_disk_name(file_hash[0], file_data['filename'])
        if not os.path.isfile(file_path):
            raise IOError('{} not found'.format(file_path))

        file_obj = File(
            checksum=file_hash[0],
            assessment_item=question,
            file_format_id=file_hash[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size = file_data['size'],
            file_on_disk=DjFile(open(file_path, 'rb')),
            preset_id=file_data['preset'],
        )
        file_obj.save()
Beispiel #30
0
def map_files_to_assessment_item(question, data):
    """ Generate files that reference the content node's assessment items """
    for file_data in data:
        file_hash = file_data['filename'].split(".")
        file_path = generate_file_on_disk_name(file_hash[0], file_data['filename'])
        if not os.path.isfile(file_path):
            raise IOError('{} not found'.format(file_path))

        file_obj = File(
            checksum=file_hash[0],
            assessment_item=question,
            file_format_id=file_hash[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size = file_data['size'],
            file_on_disk=DjFile(open(file_path, 'rb')),
            preset_id=file_data['preset'],
        )
        file_obj.save()
Beispiel #31
0
def map_files_to_node(node, data):
    """ Generate files that reference the content node """

    # filter for file data that's not empty;
    valid_data = (d for d in data if d)

    for file_data in valid_data:
        file_hash = file_data['filename'].split(".")

        # Determine a preset if none is given
        kind_preset = None
        if file_data['preset'] is None:
            kind_preset = FormatPreset.objects.filter(kind=node.kind, allowed_formats__extension__contains=file_hash[1], display=True).first()
        else:
            kind_preset = FormatPreset.objects.get(id=file_data['preset'])

        file_path=generate_file_on_disk_name(file_hash[0], file_data['filename'])
        if not os.path.isfile(file_path):
            raise IOError('{} not found'.format(file_path))

        language = None

        try:
            if file_data.get('language'):
                language = Language.objects.get(pk=file_data['language'])
        except ObjectDoesNotExist as e:
            invalid_lang = file_data.get('language')
            logging.warning("file_data with language {} does not exist.".format(invalid_lang))
            raise ValidationError("file_data given was invalid; expected string, got {}".format(invalid_lang))


        file_obj = File(
            checksum=file_hash[0],
            contentnode=node,
            file_format_id=file_hash[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size = file_data['size'],
            file_on_disk=DjFile(open(file_path, 'rb')),
            preset=kind_preset,
            language_id=file_data.get('language'),
        )
        file_obj.save()
Beispiel #32
0
def exercise_image_upload(request):
    if request.method != 'POST':
        return HttpResponseBadRequest("Only POST requests are allowed on this endpoint.")

    fobj = request.FILES.values()[0]
    assessment_item_id = request.POST.get('assessment_item_id', None)
    name, ext = os.path.splitext(fobj._name)
    get_hash(DjFile(fobj))
    file_object = File(
        preset_id=format_presets.EXERCISE_IMAGE,
        file_on_disk=DjFile(request.FILES.values()[0]),
        file_format_id=ext[1:].lower(),
        assessment_item_id=assessment_item_id,
    )
    file_object.save()
    return HttpResponse(json.dumps({
        "success": True,
        "formatted_filename": exercises.CONTENT_STORAGE_FORMAT.format(str(file_object)),
        "file_id": file_object.pk,
        "path": generate_storage_url(str(file_object)),
    }))
Beispiel #33
0
def map_files_to_assessment_item(user, question, data):
    """ Generate files that reference the content node's assessment items """
    for file_data in data:
        file_name_parts = file_data['filename'].split(".")
        file_path = generate_object_storage_name(file_name_parts[0],
                                                 file_data['filename'])
        if not os.path.isfile(file_path):
            return IOError('{} not found'.format(file_path))

        resource_obj = File(
            checksum=file_name_parts[0],
            assessment_item=question,
            file_format_id=file_name_parts[1],
            original_filename=file_data.get('original_filename') or 'file',
            source_url=file_data.get('source_url'),
            file_size=file_data['size'],
            file_on_disk=DjFile(open(file_path, 'rb')),
            preset_id=file_data['preset'],
            uploaded_by=user,
        )
        resource_obj.file_on_disk.name = file_path
        resource_obj.save()
Beispiel #34
0
def file_create(request):
    if request.method != 'POST':
        return HttpResponseBadRequest("Only POST requests are allowed on this endpoint.")

    original_filename, ext = os.path.splitext(request.FILES.values()[0]._name)
    size = request.FILES.values()[0]._size
    contentfile = DjFile(request.FILES.values()[0])
    checksum = get_hash(contentfile)
    request.user.check_space(size, checksum)

    presets = FormatPreset.objects.filter(allowed_formats__extension__contains=ext[1:].lower())
    kind = presets.first().kind
    preferences = json.loads(request.POST.get('content_defaults', None) or "{}")


    license = License.objects.filter(license_name=preferences.get('license')).first()  # Use filter/first in case preference hasn't been set
    license_id = license.pk if license else None
    new_node = ContentNode(
        title=original_filename,
        kind=kind,
        license_id=license_id,
        author=preferences.get('author') or "",
        aggregator=preferences.get('aggregator') or "",
        provider=preferences.get('provider') or "",
        copyright_holder=preferences.get('copyright_holder'),
        parent_id=settings.ORPHANAGE_ROOT_ID,
    )
    if license and license.is_custom:
        new_node.license_description = preferences.get('license_description')
    new_node.save()
    file_object = File(
        file_on_disk=contentfile,
        checksum=checksum,
        file_format_id=ext[1:].lower(),
        original_filename=request.FILES.values()[0]._name,
        contentnode=new_node,
        file_size=size,
        uploaded_by=request.user,
    )
    file_object.save()

    if kind.pk == content_kinds.VIDEO:
        file_object.preset_id = guess_video_preset_by_resolution(str(file_object.file_on_disk))
    elif presets.filter(supplementary=False).count() == 1:
        file_object.preset = presets.filter(supplementary=False).first()
    file_object.save()

    thumbnail = None
    try:
        if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \
                or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \
                or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \
                or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT:
            thumbnail = generate_thumbnail_from_node(new_node, set_node=True)
            request.user.check_space(thumbnail.file_size, thumbnail.checksum)
    except Exception:
        if thumbnail:
            thumbnail.delete()

    return HttpResponse(json.dumps({
        "success": True,
        "node": JSONRenderer().render(ContentNodeEditSerializer(new_node).data)
    }))
Beispiel #35
0
def subtitle_upload(request):
    # File will be converted to VTT format
    ext = file_formats.VTT
    language_id = request.META.get('HTTP_LANGUAGE')
    content_file = request.FILES.values()[0]

    with NamedTemporaryFile() as temp_file:
        try:
            converter = build_subtitle_converter(
                unicode(content_file.read(), 'utf-8'))
            convert_language_code = language_id

            # We're making the assumption here that language the user selected is truly the caption
            # file's language if it's unknown
            if len(converter.get_language_codes()) == 1 \
                    and converter.has_language(LANGUAGE_CODE_UNKNOWN):
                converter.replace_unknown_language(language_id)

            # determine if the request language exists by another code, otherwise we can't continue
            if not converter.has_language(convert_language_code):
                for language_code in converter.get_language_codes():
                    language = getlang_by_alpha2(language_code)
                    if language and language.code == language_id:
                        convert_language_code = language_code
                        break
                else:
                    return HttpResponseBadRequest(
                        "Language '{}' not present in subtitle file".format(
                            language_id))

            converter.write(temp_file.name, convert_language_code)
        except InvalidSubtitleFormatError as ex:
            return HttpResponseBadRequest(
                "Subtitle conversion failed: {}".format(ex))

        temp_file.seek(0)
        converted_file = DjFile(temp_file)

        checksum = get_hash(converted_file)
        size = converted_file.size
        request.user.check_space(size, checksum)

        file_object = File(
            file_size=size,
            file_on_disk=converted_file,
            checksum=checksum,
            file_format_id=ext,
            original_filename=request.FILES.values()[0]._name,
            preset_id=request.META.get('HTTP_PRESET'),
            language_id=language_id,
            uploaded_by=request.user,
        )
        file_object.save()

    return HttpResponse(
        json.dumps({
            "success":
            True,
            "filename":
            str(file_object),
            "file":
            JSONRenderer().render(FileSerializer(file_object).data)
        }))
Beispiel #36
0
def file_create(request):
    if request.method != 'POST':
        return HttpResponseBadRequest(
            "Only POST requests are allowed on this endpoint.")

    original_filename, ext = os.path.splitext(request.FILES.values()[0]._name)
    size = request.FILES.values()[0]._size
    contentfile = DjFile(request.FILES.values()[0])
    checksum = get_hash(contentfile)
    request.user.check_space(size, checksum)

    presets = FormatPreset.objects.filter(
        allowed_formats__extension__contains=ext[1:].lower())
    kind = presets.first().kind
    preferences = json.loads(
        request.POST.get('content_defaults', None) or "{}")

    license = License.objects.filter(
        license_name=preferences.get('license')).first(
        )  # Use filter/first in case preference hasn't been set
    license_id = license.pk if license else None
    new_node = ContentNode(
        title=original_filename,
        kind=kind,
        license_id=license_id,
        author=preferences.get('author') or "",
        aggregator=preferences.get('aggregator') or "",
        provider=preferences.get('provider') or "",
        copyright_holder=preferences.get('copyright_holder'),
        parent_id=settings.ORPHANAGE_ROOT_ID,
    )
    if license and license.is_custom:
        new_node.license_description = preferences.get('license_description')
    # The orphanage is not an actual tree but just a long list of items.
    with ContentNode.objects.disable_mptt_updates():
        new_node.save()
    file_object = File(
        file_on_disk=contentfile,
        checksum=checksum,
        file_format_id=ext[1:].lower(),
        original_filename=request.FILES.values()[0]._name,
        contentnode=new_node,
        file_size=size,
        uploaded_by=request.user,
    )
    file_object.save()

    if kind.pk == content_kinds.VIDEO:
        file_object.preset_id = guess_video_preset_by_resolution(
            str(file_object.file_on_disk))
    elif presets.filter(supplementary=False).count() == 1:
        file_object.preset = presets.filter(supplementary=False).first()
    file_object.save()

    thumbnail = None
    try:
        if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \
                or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \
                or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \
                or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT:
            thumbnail = generate_thumbnail_from_node(new_node, set_node=True)
            request.user.check_space(thumbnail.file_size, thumbnail.checksum)
    except Exception:
        if thumbnail:
            thumbnail.delete()

    return HttpResponse(
        json.dumps({
            "success":
            True,
            "node":
            JSONRenderer().render(ContentNodeEditSerializer(new_node).data)
        }))
Beispiel #37
0
def file_create(request):
    if request.method == 'POST':
        original_filename, ext = os.path.splitext(
            request.FILES.values()[0]._name)
        size = request.FILES.values()[0]._size
        contentfile = DjFile(request.FILES.values()[0])
        checksum = get_hash(contentfile)
        request.user.check_space(size, checksum)

        presets = FormatPreset.objects.filter(
            allowed_formats__extension__contains=ext[1:].lower())
        kind = presets.first().kind
        preferences = json.loads(request.META.get('HTTP_PREFERENCES'))
        author = preferences.get('author') or ""
        license = License.objects.filter(
            license_name=preferences.get('license')).first(
            )  # Use filter/first in case preference hasn't been set
        license_id = license.pk if license else None
        new_node = ContentNode(
            title=original_filename,
            kind=kind,
            license_id=license_id,
            author=author,
            copyright_holder=preferences.get('copyright_holder'),
        )
        if license and license.is_custom:
            new_node.license_description = preferences.get(
                'license_description')
        new_node.save()
        file_object = File(
            file_on_disk=contentfile,
            checksum=checksum,
            file_format_id=ext[1:].lower(),
            original_filename=request.FILES.values()[0]._name,
            contentnode=new_node,
            file_size=size,
            uploaded_by=request.user,
        )
        file_object.save()

        if kind.pk == content_kinds.VIDEO:
            file_object.preset_id = guess_video_preset_by_resolution(
                str(file_object.file_on_disk))
        elif presets.filter(supplementary=False).count() == 1:
            file_object.preset = presets.filter(supplementary=False).first()
        file_object.save()

        thumbnail = None
        try:
            if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \
                    or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \
                    or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \
                    or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT:
                thumbnail = generate_thumbnail_from_node(new_node,
                                                         set_node=True)
                request.user.check_space(thumbnail.file_size,
                                         thumbnail.checksum)
        except Exception:
            if thumbnail:
                thumbnail.delete()

        return HttpResponse(
            json.dumps({
                "success":
                True,
                "node":
                JSONRenderer().render(
                    ContentNodeEditSerializer(new_node).data)
            }))