Example #1
0
def general_file_upload_handle(request):
    response = {'error': 1, 'message': 0, 'url': '', 'size': ''}
    if not request.user.id:
        response['message']['login required']
        return json_response(response)

    if request.method == 'GET':
        response['message'] = 'POST request only'
        return json_response(response)

    if 'imgFile' not in request.FILES:
        response['error'] = 1
        response['message'] = 'File name error' + str(request.FILES.keys())
        return json_response(response)

    else:
        file_type = request.POST.get('file_type', 'unknow')
        dir = request.POST.get('dir', 'images')
        print 'request.get', request.GET
        try:
            return _general_file_upload_handle(request.FILES['imgFile'],
                                               file_type, dir)
        except Exception, e:
            import traceback
            response['message'] = traceback.print_exc()
            return json_response(response)
Example #2
0
def general_file_upload_handle(request):
    response = {"error": 1, "message": 0, "url": "", "size": ""}
    if not request.user.id:
        response["message"]["login required"]
        return json_response(response)

    if request.method == "GET":
        response["message"] = "POST request only"
        return json_response(response)

    if "imgFile" not in request.FILES:
        response["error"] = 1
        response["message"] = "File name error" + str(request.FILES.keys())
        return json_response(response)

    else:
        file_type = request.POST.get("file_type", "unknow")
        dir = request.POST.get("dir", "images")
        print "request.get", request.GET
        try:
            return _general_file_upload_handle(request.FILES["imgFile"], file_type, dir)
        except Exception, e:
            import traceback

            response["message"] = traceback.print_exc()
            return json_response(response)
Example #3
0
def _general_file_upload_handle(file,file_type,dir):
    response = {'error':0,'message':0,'url':'','size':''}
    date_str  = time.strftime('%Y%m%d',time.localtime())
    filepath  = os.path.join(MEDIA_ROOT,'{0}/{1}'.format(dir,date_str))
    file.name = str(time.time())+'-'+file.name
    filedata  = file.read()
    filename  = os.path.join(filepath,file.name)
    
    # make a dir
    if not os.path.exists(filepath):
        os.makedirs(filepath)

    print 'file type',file_type
    if file_type == 'image':
        im = Image.open(StringIO.StringIO(filedata))
        filename = os.path.join(filepath,'{0}.jpg'.format(md5(filedata).hexdigest()))
        im.thumbnail((600,600),Image.ANTIALIAS)
        im.convert('RGB').save(filename,'jpeg',quality=100)

    else:
        f = open(filename,'wb')
        f.write(filedata)
        f.close()

    response['error'] = 0
    response['url'] = filename.replace(MEDIA_ROOT,os.path.join(DOMAIN,MEDIA_URL))
    return json_response(response)
Example #4
0
def _general_file_upload_handle(file,file_type,dir):
    response = {'error':0,'message':0,'url':'','size':''}
    date_str  = time.strftime('%Y%m%d',time.localtime())
    filepath  = os.path.join(MEDIA_ROOT,'{0}/{1}'.format(dir,date_str))
    file.name = str(time.time())+'-'+file.name
    filedata  = file.read()
    filename  = os.path.join(filepath,file.name)
    
    # make a dir
    if not os.path.exists(filepath):
        os.makedirs(filepath)

    print 'file type',file_type
    if file_type == 'image':
        im = Image.open(StringIO.StringIO(filedata))
        filename = os.path.join(filepath,'{0}.jpg'.format(md5(filedata).hexdigest()))
        im.thumbnail((600,600),Image.ANTIALIAS)
        im.convert('RGB').save(filename,'jpeg',quality=100)

    else:
        f = open(filename,'wb')
        f.write(filedata)
        f.close()

    response['error'] = 0
    response['url'] = filename.replace(MEDIA_ROOT,os.path.join(DOMAIN,MEDIA_URL))
    return json_response(response)
Example #5
0
def _general_file_upload_handle(file, file_type, dir):
    response = {"error": 0, "message": 0, "url": "", "size": ""}
    date_str = time.strftime("%Y%m%d", time.localtime())
    filepath = os.path.join(MEDIA_ROOT, "{0}/{1}".format(dir, date_str))
    file.name = str(time.time()) + "-" + file.name
    filedata = file.read()
    filename = os.path.join(filepath, file.name)

    # make a dir
    if not os.path.exists(filepath):
        os.makedirs(filepath)

    print "file type", file_type
    if file_type == "image":
        im = Image.open(StringIO.StringIO(filedata))
        filename = os.path.join(filepath, "{0}.jpg".format(md5(filedata).hexdigest()))
        im.thumbnail((600, 600), Image.ANTIALIAS)
        im.convert("RGB").save(filename, "jpeg", quality=100)

    else:
        f = open(filename, "wb")
        f.write(filedata)
        f.close()

    response["error"] = 0
    response["url"] = filename.replace(MEDIA_ROOT, os.path.join(DOMAIN, MEDIA_URL))
    return json_response(response)
Example #6
0
def general_file_upload_handle(request):
    response = {'error':1,'message':0,'url':'','size':''}
    if request.method == 'GET':
        response['message'] = 'POST request only'
        return json_response(response)

    if 'imgFile' not in request.FILES:
        response['error'] = 1
        response['message'] = 'File name error' + str(request.FILES.keys())
        return json_response(response)

    else:
        file_type = request.POST.get('file_type','unknow')
        dir       = request.POST.get('dir','images')
        print 'request.get',request.GET
        try:
            return _general_file_upload_handle(request.FILES['imgFile'],file_type,dir)
        except Exception,e:
            import traceback
            response['message'] = traceback.print_exc()
            return json_response(response)