如何在owncloud中删除垃圾箱中的文件?

时间:2017-09-14 05:22:15

标签: python owncloud

我使用的是Python。 我删除了我的文件。 它们出现在垃圾箱中。我不知道如何从垃圾箱中删除它们。

import owncloud
oc = owncloud.Client('ip')
oc.login('account', 'password')
p=oc.list('/')
for i in p:
    oc.delete(i.path) # and than they appear in trash

2 个答案:

答案 0 :(得分:2)

目前在ownCloud中没有针对trashbin应用程序的官方API,因此它没有集成在ownCloud的python库中。然而,有几种 hacky方式可以实现您的需求。文件进入垃圾箱后,您可以使用应用程序的ajax API:

从垃圾箱中删除所有文件

如果您根本不关心垃圾箱的内容,只想删除它们:

curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php' \
    -H 'OCS-APIREQUEST: true' \
    -H 'Cookie: <session-token>' \
    --data 'allfiles=true&dir=/'

列出并有选择地删除所需的文件:

您可以在其中请求文件列表:

curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/list.php?dir=/' \
     -H 'OCS-APIREQUEST: true' \
     -H 'Cookie: <session-token>'

请注意,网址上的dir查询参数可以与您在p=oc.list('/')中列出删除所有文件时使用的相同。

响应正文将如下所示:

{
  "data": {
    "permissions": 0,
    "directory": "\/",
    "files": [
      {
        "id": 0,
        "parentId": null,
        "mtime": 1505373301000,
        "name": "ownCloud Manual.pdf",
        "permissions": 1,
        "mimetype": "application\/octet-stream",
        "size": 5111899,
        "type": "file",
        "etag": 1505373301000,
        "extraData": ".\/ownCloud Manual.pdf"
      }
    ]
  },
  "status": "success"
}

然后,您可以根据名称和mtimes创建需要删除的对象列表(files):

curl 'https://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php' \
    -H 'OCS-APIREQUEST: true' \
    -H 'Cookie: <session-token>' \
    --data 'files=["<file1>.d<mtime1>","<file2>.d<mtime2>"]&dir=/'

最后注意:请求列表并删除3个尾随零时,请求中的<mtimeX>是文件的属性"mtime": 1505373301000。另请注意通过使用 .d 连接2部分来构建名称。

希望这有帮助!

答案 1 :(得分:0)

感谢kuchiya! 我从垃圾箱中删除文件。

import requests

userID = 'aaa'
userPassword = 'bbbbbbb'

with requests.Session() as s:
    response = s.get('http://'+userID+':'+userPassword+'@<owncloud-URL>/index.php/apps/files/?dir=/&fileid=4896' ) #change your fileid 
    token = response.content.split('data-requesttoken="')[1].split('"')[0]

    Cookie = 'oc7b6t1tjo61='+s.cookies['oc7b6t1tjo61']+';oc_sessionPassphrase='+s.cookies['oc_sessionPassphrase']

    data = {'allfiles':'true', 'dir':'/'}
    headers = {'requesttoken':token, 'OCS-APIREQUEST':'true', 'Cookie':Cookie ,'Accept-Language':'zh-TW,en-US;q=0.7,en;q=0.3'}
    response2 = s.post('http://<owncloud-URL>/index.php/apps/files_trashbin/ajax/delete.php',data = data, headers = headers, cookies=s.cookies)
    print response2.content
相关问题