예제 #1
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)
            }))
예제 #2
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)
            }))
예제 #3
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)
    }))
예제 #4
0
파일: files.py 프로젝트: toccotedd/studio
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)
        }))