Exemplo n.º 1
0
def cron_sync():
    print 'start cron_sync ...'
    with app.test_request_context():
        users = User.query.all()
        for user in users:
            print user
            # make sure not conflict
            if get_user_last_activity(user.id) is not None:
                continue
            resp = client.metadata(user.vdisk_token, '/idesktop')
            if isinstance(resp, str):
                print 'error in metadata'
                continue
            result = json.loads(resp.read())
            if result['hash'] == user.vdisk_hash:
                print 'same hash'
                continue
            user.vdisk_hash = result['hash']
            db.session.add(user)
            db.session.commit()

            contents = [content for content in result['contents'] if is_good(content)]
            for content in contents:
                #print content['md5']
                pic = Picture.query.filter_by(hash_id=content['md5']).first()
                if not pic:  # need to upload
                    #upload to upyun
                    ret = client.media(user.vdisk_token, content['path'])
                    if isinstance(ret, str):
                        continue
                    url = json.loads(ret.read())['url']
                    filename = content['md5']
                    filename += '.' + content['path'].split('.')[-1]
                    low_q.enqueue(down_upload, url, filename)

                    # add to db
                    filename = content['path'].split('/')[-1]
                    picture = Picture(filename, content['md5'])
                    picture.in_yun = True
                    picture.user = user
                    picture.users.append(user)
                    db.session.add(picture)
                    db.session.commit()
                    #print filename + '#' + url
                elif user not in pic.users:
                    pic.users.append(user)
                    db.session.add(pic)
                    db.session.commit()
            #handle self delete in local folder
            hashs = [content['md5'] for content in contents]
            for pic in user.downloads.all():
                if pic.hash_id not in hashs:
                    #need delete
                    pic.users.remove(user)
                    db.session.add(pic)
                    db.session.commit()
    print 'stop cron_sync ...'
    ret = {'result':'ok'}
    return jsonify(**ret)
Exemplo n.º 2
0
def sync():
    resp = client.metadata(g.user.vdisk_token, '/idesktop')
    if isinstance(resp, str):
        flash('get metadata failed!', 'error')
        return redirect(url_for('setting'))
    result = json.loads(resp.read())
    user = g.user
    user.vdisk_hash = result['hash']
    db.session.add(user)
    db.session.commit()
    #handle self add file in local folder
    contents = [content for content in result['contents'] if is_good(content)]
    for content in contents:
        #print content['md5']
        pic = Picture.query.filter_by(hash_id=content['md5']).first()
        if not pic:  # need to upload
            #upload to upyun
            ret = client.media(g.user.vdisk_token, content['path'])
            if isinstance(ret, str):
                continue
            url = json.loads(ret.read())['url']
            filename = content['md5']
            filename += '.' + content['path'].split('.')[-1]
            #get_queue('default').enqueue(down_upload, url, filename)
            low_q.enqueue(down_upload, url, filename)
            #down_upload(url, filename)
            # add to db
            filename = content['path'].split('/')[-1]
            picture = Picture(filename, content['md5'])
            picture.in_yun = True
            picture.user = g.user
            picture.users.append(g.user)
            db.session.add(picture)
            db.session.commit()
            #print filename + '#' + url
        elif g.user not in pic.users:
            pic.users.append(g.user)
            db.session.add(pic)
            db.session.commit()
    #handle self delete in local folder
    hashs = [content['md5'] for content in contents]
    for pic in g.user.downloads.all():
        if pic.hash_id not in hashs:
            #need delete
            pic.users.remove(g.user)
            db.session.add(pic)
            db.session.commit()
    flash('sync completed!', 'success')
    return redirect(url_for('setting'))