示例#1
0
    def create(self, request):
        '''
        Image upload
        '''
        queryset = Gallery.objects.filter(family_id=request.user.family_id)
        gallery_id, gallery_id_valid = intTryParse(
            request.data.get("gallery_id"))

        if not gallery_id_valid:
            raise ParseError('Invalid gallery_id')

        # Check gallery is part of family
        gallery = get_object_or_404(queryset, pk=gallery_id)

        try:
            uploaded = request.FILES['picture']
            name, ext = os.path.splitext(uploaded.name)

            if uploaded.size > MAX_FILE_SIZE:
                raise ParseError('File too big')

            filename = create_hash(uploaded.name) + '.jpg'
            image = Image(gallery_id=gallery.id,
                          family_id=gallery.family_id,
                          title=name,
                          uploaded_by=request.user)

            path = upload_to(image, filename)

            #Write the file to the destination
            destination = open(os.path.join(settings.MEDIA_ROOT, path), 'wb+')

            for chunk in uploaded.chunks():
                destination.write(chunk)
            destination.close()

            image.original_image = path
            PIL.Image.open(
                os.path.join(settings.MEDIA_ROOT,
                             str(image.original_image))).verify()
            image.save()

            image.upload_files_to_s3()
            image.delete_local_image_files()

            create_message('image_face_detect', image.id)

            serializer = ImageSerializer(image)
            return Response(serializer.data)

        except Exception as e:

            if image:
                image.delete_local_image_files()
                image.delete()

            raise ParseError(str(e))
示例#2
0
def process_image(filename, file, gallery):
    '''
    Processes each image file
    '''
    name, ext = os.path.splitext(file.name)
    filename =  create_hash(name) +'.jpg'

    im = Image(gallery_id=gallery.id, family_id=gallery.family_id, title=name)
    upload_name = upload_to(im, filename)

    result = {
        'name': basename(name),
        'size': file.size,
        'url': settings.MEDIA_URL + str(upload_name),
        'filename': filename
    }

    if file.size > MAX_FILE_SIZE:
        result['error'] = tran('File is too big')
        return result

    #Write the file to the destination
    destination = open(os.path.join(settings.MEDIA_ROOT, str(upload_name)), 'wb+')

    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()

    im.original_image = upload_name

    #Check this is a valid image
    try:
        PIL.Image.open(os.path.join(settings.MEDIA_ROOT, str(im.original_image))).verify()
        im.save()
        im.upload_files_to_s3()
        im.delete_local_image_files()

        result['image_id'] = im.id


    except:
        im.delete_local_image_files()
        result['error'] = tran('Invalid image!')

    return result