コード例 #1
0
ファイル: importing.py プロジェクト: thechild/mbox2sql
def handle_attachment(message, content, related=False):
#    r = ''
#    if related:
#        r = '(r)'

    filename, encoding = decode_header(content.get_filename())[0]
    if encoding:
        filename = filename.decode(encoding, errors='replace')

    #if not related:
    #    print "saving attachment [%s] of type %s from message %d %s" % (filename, content.get_content_type(), message.id, r)

    a = Attachment()
    a.filename = filename  # TODO need to parse weird strings from this
    if not a.filename:
        a.filename = str(uuid.uuid4())
    a.content_type = content.get_content_type()
    a.stored_location = os.path.join(files_dir, str(message.id), get_valid_filename(a.filename))
        # probably want to fix this too
    a.mime_related = related
        # load the file
    file_content = content.get_payload(decode=1)
    a.file_md5 = hashlib.md5(file_content).hexdigest()  # again, probably a better way to do this than all in memory
    # actually write it do disk - should wrap this in a try except too
    if not os.path.exists(os.path.join(files_dir, str(message.id))):
        os.makedirs(os.path.join(files_dir, str(message.id)))
    with open(a.stored_location, 'wb') as fp:
        fp.write(file_content)
    a.message = message
    a.save()
コード例 #2
0
ファイル: attachments.py プロジェクト: larrykubin/showlists
async def sign(fileUpload: FileUpload,
               current_user: User = Depends(get_current_user)):
    # create attachment record with status incomplete
    # if upload success, callback from uppy
    try:
        attachment = Attachment()
        attachment.filename = fileUpload.filename
        attachment.creator_id = current_user.id
        db.add(attachment)
        db.commit()

        bucket_name = 'showlists'
        object_name = 'shows/{}'.format(attachment.id)
        response = s3_client.generate_presigned_post(bucket_name, object_name)
    except ClientError as e:
        print(e)
        return None

    # The response contains the presigned URL and required fields
    return response
コード例 #3
0
def add_attachment():

    api_key = request.headers.get('Authorization')
    response = UserClient.get_user(api_key)
    if not response:
        return make_response(jsonify({'message': 'Not logged in'}), 401)

    user = response['result']
    if int(user['id']) != int(request.form['contractor_user_id']):
        return make_response(
            jsonify({'message': 'user can not add an attachment'}), 401)
    attachment_instance = Attachment()
    attachment_instance.name = request.form['title']
    attachment_instance.filename = request.form['filename']
    attachment_instance.contract_id = request.form['contract_id']
    attachment_instance.description = request.form['description']

    db.session.add(attachment_instance)
    db.session.commit()

    response = jsonify({'message': 'success'})

    return response
コード例 #4
0
ファイル: api.py プロジェクト: Red54/Sophia
def upload_file():
    file = request.files['file']
    if file and allowed_file(file.filename):
        ext_name = file.filename.rsplit('.', 1)[1]
        filename = '{0:s}.{1:s}'.format(uuid.uuid1(), ext_name)
        file.save(os.path.join(UPLOAD_FOLDER, filename))
        size = os.path.getsize(os.path.join(UPLOAD_FOLDER, filename))
        attachment = Attachment()
        attachment.path = filename
        attachment.filename = file.filename
        attachment.size = size
        attachment.created_at = int(time.time())
        attachment.user_id = current_user.id
        attachment.ext_name = ext_name
        attachment.topic_id = 0
        db.session.add(attachment)
        db.session.commit()
        obj = {
            'id': attachment.id,
            'url': url_for('uploaded_file', filename=filename)
        }
        return json.dumps(obj)
    return ''