Beispiel #1
0
 def save_attachment(self, post, memfile):
     if memfile:
         obj = Attachment(size=memfile.size,
                          content_type=memfile.content_type,
                          name=memfile.name,
                          post=post)
         dir = os.path.join(settings.MEDIA_ROOT,
                            ydata_settings.ATTACHMENT_UPLOAD_TO)
         fname = '%d.0' % post.id
         path = os.path.join(dir, fname)
         file(path, 'w').write(memfile.read())
         obj.path = fname
         obj.save()
Beispiel #2
0
def upload_single_file(req, id):
    ''' 上传单个附件, 只作异步上传使用
    1. 上传的 form 中文件的 name = 'single_file'
    '''

    if req.method != 'POST':
        return HttpResponse("Just for POST a single file")

    f = req.FILES['single_file']  #req.FILES.values()[0]
    if f.size > ydata_settings.ATTACHMENT_SIZE_LIMIT:
        notes = u'文件太大: %s (限制大小: %s)' % (f.size,
                                          ydata_settings.ATTACHMENT_SIZE_LIMIT)
        r = u'<script type="text/javascript">window.parent.UploadError("%s");</script>' % notes
        return HttpResponse(r)

    #topic = get_object_or_404(Topic, pk=id)
    try:
        topic = Topic.objects.get(pk=id)
    except Topic.DoesNotExist:
        topic = False

    sha1_hash = hashlib.sha1()
    for chunk in f.chunks():
        sha1_hash.update(chunk)
    fhash = sha1_hash.hexdigest()

    attachments = Attachment.objects.filter(hash=fhash)
    notes = ''

    if attachments:
        # 此文件在服务器上已存在, 增加引用计数即可
        local_name = ''
        attachment = False
        for a in attachments:
            if local_name == '':
                local_name = a.path
            if a.user == req.user:
                attachment = a
                notes = u'文件已经存在,请不要重复上传。'
            a.qtimes += 1
            a.save()
        if not attachment:
            attachment = Attachment(user=req.user,
                                    name=f.name,
                                    path=local_name,
                                    size=f.size,
                                    hash=fhash,
                                    qtimes=2)
            if topic:
                attachment.topic = topic

    else:
        local_name = str(id) + '-' + str(req.user.id) + '-' + f.name
        save_path = os.path.join(settings.MEDIA_ROOT,
                                 ydata_settings.ATTACHMENT_UPLOAD_TO,
                                 local_name)
        save_file = open(save_path, 'wb+')
        for chunk in f.chunks():
            save_file.write(chunk)

        attachment = Attachment(user=req.user,
                                name=f.name,
                                path=local_name,
                                size=f.size,
                                hash=fhash)
        if topic:
            attachment.topic = topic

    attachment.save()
    r = u'<script type="text/javascript">window.parent.AfterSubmit("%s","%s","%s","%s","%s");</script>'\
        % (attachment.id, attachment.name,
           reverse ('ydata:show_attachment', args=[attachment.id]),
           reverse ('ydata:download_attachment', args=[attachment.id]),
           notes)

    print r
    return HttpResponse(r)
Beispiel #3
0
def upload_single_file(req, id):
    ''' 上传单个附件, 只作异步上传使用
    1. 上传的 form 中文件的 name = 'single_file'
    '''

    if req.method != 'POST':
        return HttpResponse("Just for POST a single file")

    f = req.FILES['single_file'] #req.FILES.values()[0]
    if f.size > ydata_settings.ATTACHMENT_SIZE_LIMIT:
        notes = u'文件太大: %s (限制大小: %s)' % (f.size, ydata_settings.ATTACHMENT_SIZE_LIMIT)
        r = u'<script type="text/javascript">window.parent.UploadError("%s");</script>' % notes
        return HttpResponse(r)

    #topic = get_object_or_404(Topic, pk=id)
    try:
        topic = Topic.objects.get(pk=id)
    except Topic.DoesNotExist:
        topic = False

    sha1_hash = hashlib.sha1()
    for chunk in f.chunks():
        sha1_hash.update(chunk)
    fhash = sha1_hash.hexdigest()

    attachments = Attachment.objects.filter(hash=fhash)
    notes = ''

    if attachments:
        # 此文件在服务器上已存在, 增加引用计数即可
        local_name = ''
        attachment = False
        for a in attachments:
            if local_name == '':
                local_name = a.path
            if a.user == req.user:
                attachment = a
                notes = u'文件已经存在,请不要重复上传。'
            a.qtimes += 1
            a.save()
        if not attachment:
            attachment = Attachment(user=req.user, name=f.name, path=local_name, size=f.size, hash=fhash, qtimes=2)
            if topic:
                attachment.topic = topic

    else:
        local_name = str(id) + '-' + str(req.user.id) + '-' + f.name
        save_path = os.path.join(settings.MEDIA_ROOT,
                                 ydata_settings.ATTACHMENT_UPLOAD_TO,
                                 local_name)
        save_file = open(save_path, 'wb+')
        for chunk in f.chunks():
            save_file.write(chunk)

        attachment = Attachment(user=req.user, name=f.name, path=local_name, size=f.size, hash=fhash)
        if topic:
            attachment.topic = topic

    attachment.save()
    r = u'<script type="text/javascript">window.parent.AfterSubmit("%s","%s","%s","%s","%s");</script>'\
        % (attachment.id, attachment.name,
           reverse ('ydata:show_attachment', args=[attachment.id]),
           reverse ('ydata:download_attachment', args=[attachment.id]),
           notes)

    print r
    return HttpResponse(r)