Dropbox webhook,检测已删除的文件

Dropbox webhook, detect deleted file

我正在尝试检测我自己的保管箱应用程序文件夹中的文件是否被删除。 所以,我设置了一个 webhook 来接收通知,我使用 DropboxClient.delta 方法来了解实际发生了什么变化。 根据文档,delta方法returns一个有4个键的字典,'entries'键包含A list of "delta entries"。删除文件时,条目是 [path, metadata](路径是字符串,元数据是字典),元数据是 None。但我从来没有把它变成 None,即使文件实际上已从目录中删除。 这是我的 webhook 通知接收器:

@app.route('/webhook', methods=['POST', 'GET'])
def webhook():
    if request.method == 'GET':
        return request.args.get('challenge')

    fh = open('dropbox.txt', 'a') # log file
    client = dropbox.client.DropboxClient('<access token>') 

    for uid in json.loads(request.data)['delta']['users']:
        fh.write('User %d is doing something:\n' % uid)

        cursor = None
        has_more = True

        while has_more:
            result = client.delta(cursor)
            i = 1

            for path, metadata in result['entries']:
                fh.write('Metatadata: %s\n' % json.dumps(metadata) )
                i = i + 1   

            cursor = result['cursor']
            has_more = result['has_more']

        fh.write('End logging activities (#%d) of user %d\n' % (i - 1, uid))

    fh.close()
    return ''

所以,我在我的目录中添加了一些文件 (3),在我的日志文件中我可以看到:

User <uid> is doing something:
Metatadata: {"revision": 3, "bytes": 3952, "thumb_exists": false, "rev": "33bb68089", "modified": "Fri, 28 Aug 2015 13:40:32 +0000", "mime_type": "text/x-python", "path": "/a.py", "is_dir": false, "size": "3.9 KB", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:32 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 4, "bytes": 812, "thumb_exists": false, "rev": "43bb68089", "modified": "Fri, 28 Aug 2015 13:40:34 +0000", "mime_type": "text/x-python", "path": "/b.py", "is_dir": false, "size": "812 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:34 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 5, "bytes": 295, "thumb_exists": false, "rev": "53bb68089", "modified": "Fri, 28 Aug 2015 13:40:35 +0000", "mime_type": "text/x-python", "path": "/c.py", "is_dir": false, "size": "295 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:35 +0000", "icon": "page_white_code"}
End logging activities (#3) of user <uid>

我删除了一个文件 (c.py),在我的日志文件中我看到:

User <uid> is doing something:
Metatadata: {"revision": 3, "bytes": 3952, "thumb_exists": false, "rev": "33bb68089", "modified": "Fri, 28 Aug 2015 13:40:32 +0000", "mime_type": "text/x-python", "path": "/a.py", "is_dir": false, "size": "3.9 KB", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:32 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 4, "bytes": 812, "thumb_exists": false, "rev": "43bb68089", "modified": "Fri, 28 Aug 2015 13:40:34 +0000", "mime_type": "text/x-python", "path": "/c.py", "is_dir": false, "size": "812 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:34 +0000", "icon": "page_white_code"}
End logging activities (#2) of user <uid>

每次目录中发生变化时,我都会得到该目录中所有文件的列表以及未修改的文件。虽然没有删除的痕迹。 我以为我只会收到修改或删除的文件。

这是正常行为吗? 谢谢。

您似乎没有在通知之间跟踪光标。所以每次有变化时,你都会用一个空光标调用 delta,这意味着 "Tell me about all the files." 你想要做的是用上次获得的光标调用它,这意味着"Tell me what's changed since (cursor)."

您需要将光标保存在服务器上的某处(例如数据库中),并确保在向 Dropbox 请求更改时始终使用最新的光标。