Example #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)
Example #2
0
def uploadImage(request):
    global is_error, myfile
    is_error = 0
    if request.user.is_authenticated():
        if request.user.is_superuser:
            if request.POST:
                libelle = request.POST["libelle"]
                type = request.POST["type"]
                description = request.POST["descript"]
                save_plus = request.POST.getlist('save_and')
                if libelle == "":
                    error_libelle = "veuillez remplir ce champs"
                    is_error = 1
                if int(type) == 0:
                    error_type = "veuillez selectionner un type"
                    is_error = 1
                try:
                    myfile = request.FILES['image']
                except:
                    error_logo = "veuillez selectionner une image"
                    is_error = 1
                if is_error != 0:
                    return render(request, 'uploadImage.html', locals())
                else:
                    save_path = settings.MEDIA_ROOT
                    last = myfile.name[myfile.name.rfind("."):len(myfile.name)]
                    fs = FileSystemStorage()
                    currentId = 0
                    try:
                        image = Image.objects.last()
                        currentId = image.idImage + 1
                    except:
                        currentId = 1
                    save_name = "teranga-food-upload-0" + str(currentId) + last
                    fs.save(settings.MEDIA_ROOT + save_name, myfile)
                    myImage = Image()
                    myImage.description = description
                    myImage.libelle = libelle
                    myImage.type = int(type)
                    myImage.saveName = "/media/" + save_name
                    myImage.save()
                    try:
                        _next = int(save_plus[0])
                    except:
                        _next = 0
                    if _next == 0:
                        return redirect(listeImage)
                    else:
                        libelle = ""
                        description = ""
                        return render(request, 'uploadImage.html', locals())
            else:
                return render(request, 'uploadImage.html', locals())
Example #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)
Example #4
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)
Example #5
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':
            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)
Example #6
0
    def post(self, request):
        all_category = Category.objects.all()
        images = request.FILES.getlist("image_file")
        categorys = request.POST.getlist("image_category")
        description = request.POST.get("image_description")
        tags = request.POST.get('image_tag', '')
        if tags != '' and tags.find('|'):
            tags = tags.split('|')

        all_file_url = []
        upload_path = os.path.join(settings.MEDIA_ROOT, 'release')
        upload_path_thumb = os.path.join(settings.MEDIA_ROOT, 'release_thumb')
        if images:
            for image in images:
                md5 = hashlib.md5()
                for chrunk in image.chunks():
                    md5.update(chrunk)
                name = image.name
                size = image.size
                type = os.path.splitext(name)[-1]
                md5_name = md5.hexdigest()

                img_path = os.path.join(upload_path, md5_name) + type
                img_path_thumb = os.path.join(upload_path_thumb,
                                              md5_name) + type

                url = os.path.join(settings.MEDIA_URL, 'release',
                                   md5_name) + type
                url_thumb = os.path.join(settings.MEDIA_URL, 'release_thumb',
                                         md5_name) + type

                all_file_url.append(url_thumb)

                img = open(img_path, 'wb')
                for chrunk in image.chunks():
                    img.write(chrunk)
                img.close()

                pimg = PImage.open(img_path)

                _img = Image()
                _img.user = request.user
                _img.name = name
                _img.description = description
                _img.url = url
                _img.url_thumb = url_thumb
                _img.size = size
                _img.width, _img.height = pimg.size
                _img.type = type.strip('.')
                _img.save()

                pimg.thumbnail((300, 300))
                pimg.save(img_path_thumb)

                if len(categorys) == 0:
                    category = Category.objects.get(name='Other')
                    category.count += 1
                    category.save()
                    _img.categorys.add(category)
                else:
                    for category_id in categorys:
                        category = Category.objects.get(pk=category_id)
                        category.count += 1
                        category.save()
                        _img.categorys.add(category)

                if len(tags) == 0:
                    tag = Tag.objects.get(name='Other')
                    tag.count += 1
                    tag.save()
                    _img.tags.add(tag)

                else:
                    for tag_name in tags:
                        tag = None
                        try:
                            tag = Tag.objects.get(name=tag_name)
                        except:
                            pass
                        if tag:
                            tag.count += 1
                            tag.save()
                            _img.tags.add(tag)
                        else:
                            tag = Tag()
                            tag.name = tag_name
                            tag.user = request.user
                            tag.count += 1
                            tag.save()
                            _img.tags.add(tag)
                _img.save()

            return render(
                request, 'user/release.html', {
                    'message': '文件上传成功!',
                    'all_file_url': all_file_url,
                    'all_category': all_category,
                })
        else:
            return render(request, 'user/release.html', {
                'message': '请先选择需要上传的文件!',
                'all_category': all_category,
            })