Beispiel #1
0
def create_company_fileobj(request, company_id, bucket=QN_COMPANY_ICON_BUCKET):
    """
    创建公司文件
    by:尚宗凯 at:2015-06-17
	修改文件名称
	by:尚宗凯 at:2015-06-23
    """
    fileobj = ComFile()
    fileobj.company_id = company_id
    fileobj.user_id = request.user.pk
    fileobj.bucket = bucket
    fileobj.file_status = False
    fileobj.filetype = request.REQUEST.get('filetype', 'file')
    fileobj.size = int(request.REQUEST.get('size', '0'))
    fileobj.name = request.REQUEST.get('filename', '')[-50:]
    filename =  str(uuid.uuid4()) + '.' + fileobj.name.split('.')[-1]
    object_name = str('/%s/%s' % ('company', filename))
    fileobj.fileurl = object_name
    fileobj.save()
    return fileobj
Beispiel #2
0
def image_up(request, company_id=None):
    """
    在线编辑器,上传图片。如果是系统新闻,则 company_id 为None
    如果 是系统图片则,另存到 qn-webimage中
    by:王健 at:2015-06-14
    保存图片到七牛
    by:王健 at:2015-06-17
    :param request:
    :param company_id:
    :return:
    """
    result = getResult()
    title = request.REQUEST.get('title', '')
    f = request.FILES[fieldname]
    filename = str(uuid.uuid4()) + '.' + f.name.split('.')[-1]
    if not isAllowFiles(f.name, imgtype):
        result['state'] = errorInfo['TYPE']
    elif not isAllowSize(f.size, maxSize):
        result['state'] = errorInfo['SIZE']
    else:
        if company_id:
            fileobj = ComFile()
            fileobj.bucket = QN_COMPANY_ICON_BUCKET
            fileobj.company_id = company_id
            object_name = str('/%s/%s' % (fileobj.company_id, filename))
        else:
            fileobj = SysFile()
            fileobj.bucket = QN_WEBIMAGE_ICON_BUCKET
            object_name = str('/%s/%s' % ('sys', filename))

        try:

            fileobj.user_id = request.user.pk

            fileobj.file_status = False
            fileobj.filetype = f.name.lower().split('.')[-1]
            fileobj.size = int(request.REQUEST.get('size', '0'))
            fileobj.name = filename
            fileobj.fileurl = object_name
            fileobj.save()
            #todo:将图片数据”data“ 保存到 七牛服务器。此处需要使用七牛的sdk
            jsondata = fileobj.get_qn_params()
            import qiniu
            r = qiniu.put_stream(jsondata['token'], jsondata['key'], f, f.size)
            if r[1].status_code != 200:
                raise

        except Exception, e:
            fileobj.delete()
            result['state'] = errorInfo['DIR']

        else:
Beispiel #3
0
def image_up(request, company_id=None):
    """
    在线编辑器,上传图片。如果是系统新闻,则 company_id 为None
    如果 是系统图片则,另存到 qn-webimage中
    by:王健 at:2015-06-14
    保存图片到七牛
    by:王健 at:2015-06-17
    :param request:
    :param company_id:
    :return:
    """
    result = getResult()
    title = request.REQUEST.get('title', '')
    f = request.FILES[fieldname]
    filename =  str(uuid.uuid4()) + '.' + f.name.split('.')[-1]
    if not isAllowFiles(f.name, imgtype):
        result['state'] = errorInfo['TYPE']
    elif not isAllowSize(f.size, maxSize):
        result['state'] = errorInfo['SIZE']
    else:
        if company_id:
            fileobj = ComFile()
            fileobj.bucket = QN_COMPANY_ICON_BUCKET
            fileobj.company_id = company_id
            object_name = str('/%s/%s' % (fileobj.company_id, filename))
        else:
            fileobj = SysFile()
            fileobj.bucket = QN_WEBIMAGE_ICON_BUCKET
            object_name = str('/%s/%s' % ('sys', filename))

        try:

            fileobj.user_id = request.user.pk

            fileobj.file_status = False
            fileobj.filetype = f.name.lower().split('.')[-1]
            fileobj.size = int(request.REQUEST.get('size', '0'))
            fileobj.name = filename
            fileobj.fileurl = object_name
            fileobj.save()
            #todo:将图片数据”data“ 保存到 七牛服务器。此处需要使用七牛的sdk
            jsondata = fileobj.get_qn_params()
            import qiniu
            r = qiniu.put_stream(jsondata['token'], jsondata['key'], f, f.size)
            if r[1].status_code != 200:
                raise

        except Exception, e:
            fileobj.delete()
            result['state'] = errorInfo['DIR']

        else: