Exemple #1
0
def upload(request):
    if request.method == 'GET':
        return render_to_response('upload.html', {'url': request.GET.get('url', ''),}, context_instance=RequestContext(request))
    elif request.method == 'POST':
        tmp = tempfile.mkstemp()
        md5 = hashlib.md5()
        fext = ""
        orig = ""
        
        if request.POST['upload_type'] == 'file':
            file = request.FILES['upload_file']
            fext = file.name[-3:]
            orig = file.name
            f = os.fdopen(tmp[0], "wb+")
            for chunk in file.chunks():
                f.write(chunk)
                md5.update(chunk)
            f.close()

        elif request.POST['upload_type'] == 'url':
            upload_url = request.POST['upload_url']
            remote_image = urllib2.urlopen(upload_url)
            data = remote_image.read()
            md5.update(data)
            fext = request.POST['upload_url'][-3:]
            orig = request.POST['upload_url']
            
            f = os.fdopen(tmp[0], "wb+")
            f.write(data)
            f.close()

        img = Image()
        try:
            next_id = Image.objects.order_by('-id')[0].id + 1
        except IndexError:
            next_id = settings.IMAGE_ID_OFFSET + 1

        img.id = next_id
        img.base62 = base62(next_id)
        img.filename = base62(next_id) + "." + fext.lower()
        img.orig_filename = orig
        img.type = '' # todo
        img.description = '' # not implemented yet.
        img.uploader = request.user
        img.md5sum = md5.hexdigest()
        image_file = os.path.join(settings.MEDIA_ROOT,img.filename)
        thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)
            
        try:
            img.save()
        except IntegrityError, e:
            os.unlink(tmp[1]) # delete the uploaded file if it already exists
            return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename)

        shutil.move(tmp[1], image_file)
        os.system("/usr/bin/convert %s -thumbnail 150x150 %s" % (image_file, thumbnail))

        return HttpResponseRedirect(settings.MEDIA_URL + img.filename)
Exemple #2
0
def image_handler(files, request):
    tmp = tempfile.mkstemp()
    md5 = hashlib.md5()
    if request.POST['upload_type'] == 'file':
        orig = files.name
        fext = orig[orig.rfind('.') + 1:]
        f = os.fdopen(tmp[0], "wb+")
        for chunk in files.chunks():
            f.write(chunk)
            md5.update(chunk)
        f.close()
    elif request.POST['upload_type'] == 'url':
        md5.update(files)
        fext = request.POST['upload_url'][-3:]
        orig = request.POST['upload_url']

        f = os.fdopen(tmp[0], "wb+")
        f.write(files)
        f.close()

    img = Image()
    try:
        next_id = Image.objects.order_by('-id')[0].id + 1
    except IndexError:
        next_id = settings.IMAGE_ID_OFFSET + 1

    img.id = next_id
    img.base62 = base62(next_id)
    img.filename = base62(next_id) + "." + fext.lower()
    img.orig_filename = orig
    img.type = ''  # todo
    img.description = ''  # not implemented yet.
    img.uploader = request.user
    img.md5sum = md5.hexdigest()
    image_file = os.path.join(settings.MEDIA_ROOT, img.filename)
    thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)

    try:
        img.save()
    except IntegrityError, e:
        os.unlink(tmp[1])  # delete the uploaded file if it already exists
        return HttpResponseRedirect(settings.MEDIA_URL + Image.objects.get(
            md5sum=img.md5sum).filename)
Exemple #3
0
def image_handler(files, request):
    tmp = tempfile.mkstemp()
    md5 = hashlib.md5()
    if request.POST['upload_type'] == 'file':
        orig = files.name
        fext = orig[orig.rfind('.')+1:]
        f = os.fdopen(tmp[0], "wb+")
        for chunk in files.chunks():
            f.write(chunk)
            md5.update(chunk)
        f.close()
    elif request.POST['upload_type'] == 'url':
        md5.update(files)
        fext = request.POST['upload_url'][-3:]
        orig = request.POST['upload_url']
                                            
        f = os.fdopen(tmp[0], "wb+")
        f.write(files)
        f.close()

    img = Image()
    try:
        next_id = Image.objects.order_by('-id')[0].id + 1
    except IndexError:
        next_id = settings.IMAGE_ID_OFFSET + 1

    img.id = next_id
    img.base62 = base62(next_id)
    img.filename = base62(next_id) + "." + fext.lower()
    img.orig_filename = orig
    img.type = '' # todo
    img.description = '' # not implemented yet.
    img.uploader = request.user
    img.md5sum = md5.hexdigest()
    image_file = os.path.join(settings.MEDIA_ROOT,img.filename)
    thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)

    try:
        img.save()
    except IntegrityError, e:
        os.unlink(tmp[1]) # delete the uploaded file if it already exists
        return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename)
def upload(request):
    if request.method == 'GET':
        return render_to_response('upload.html', {
            'url': request.GET.get('url', ''),
        },
                                  context_instance=RequestContext(request))
    elif request.method == 'POST':
        tmp = tempfile.mkstemp()
        md5 = hashlib.md5()
        fext = ""
        orig = ""

        if request.POST['upload_type'] == 'file':
            file = request.FILES['upload_file']
            fext = file.name[-3:]
            orig = file.name
            f = os.fdopen(tmp[0], "wb+")
            for chunk in file.chunks():
                f.write(chunk)
                md5.update(chunk)
            f.close()

        elif request.POST['upload_type'] == 'url':
            remote_image = urllib2.urlopen(request.POST['upload_url'])
            data = remote_image.read()
            md5.update(data)
            fext = request.POST['upload_url'][-3:]
            orig = request.POST['upload_url']

            f = os.fdopen(tmp[0], "wb+")
            f.write(data)
            f.close()

        img = Image()
        try:
            next_id = Image.objects.order_by('-id')[0].id + 1
        except IndexError:
            next_id = settings.IMAGE_ID_OFFSET + 1

        img.base62 = base62(next_id)
        img.filename = base62(next_id) + "." + fext.lower()
        img.orig_filename = orig
        img.type = ''  # todo
        img.description = ''  # not implemented yet.
        img.uploader = request.user
        img.md5sum = md5.hexdigest()
        image_file = os.path.join(settings.MEDIA_ROOT, img.filename)
        thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)

        try:
            img.save()
        except IntegrityError:
            os.unlink(tmp[1])  # delete the uploaded file if it already exists
            return HttpResponseRedirect(settings.MEDIA_URL + Image.objects.get(
                md5sum=img.md5sum).filename)

        shutil.move(tmp[1], image_file)
        os.system("/usr/bin/convert %s -thumbnail 150x150 %s" %
                  (image_file, thumbnail))

        return HttpResponseRedirect(settings.MEDIA_URL + img.filename)