Working share with nextcloud
This commit is contained in:
parent
0fb3c2d178
commit
85e02a16b4
1 changed files with 25 additions and 26 deletions
|
@ -10,7 +10,7 @@ import uuid
|
|||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from os import environ as env
|
||||
from os import path, makedirs
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import urlparse, quote_plus
|
||||
|
||||
import pwd
|
||||
import requests
|
||||
|
@ -61,7 +61,7 @@ class OCM:
|
|||
if self.legacy_mode:
|
||||
return {
|
||||
"name": self.config[self.user][provider_id]["name"],
|
||||
"owner": self.user,
|
||||
"owner": self.user + '@localhost.local',
|
||||
"permission":
|
||||
self.config[self.user][provider_id]["permission"],
|
||||
"protocol": {
|
||||
|
@ -110,7 +110,7 @@ class OCM:
|
|||
def receive(self):
|
||||
self.server.serve_forever()
|
||||
|
||||
def create_share(self, receiver, resource_type, name) -> str:
|
||||
def create_share(self, receiver, resource_type, name, share_endpoint) -> None:
|
||||
provider_id = uuid.uuid4().hex
|
||||
self.config[self.user][provider_id] = {
|
||||
"name": name,
|
||||
|
@ -121,17 +121,33 @@ class OCM:
|
|||
"token": uuid.uuid4().hex,
|
||||
}
|
||||
self.write_config()
|
||||
payload = self.get_share_payload(provider_id)
|
||||
print(payload)
|
||||
|
||||
return provider_id
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
share_req = requests.post(
|
||||
share_endpoint,
|
||||
headers=headers,
|
||||
json=payload)
|
||||
if share_req.status_code != 200:
|
||||
print("Error: " + str(share_req.status_code))
|
||||
print(share_req.text)
|
||||
sys.exit(1)
|
||||
share_data = share_req.json()
|
||||
|
||||
print(share_data)
|
||||
|
||||
def share(self, file, directory, to):
|
||||
uparse = urlparse(to)
|
||||
receiver = uparse.username
|
||||
if not receiver:
|
||||
cloudid = f'{uparse.username}@{uparse.hostname}'
|
||||
if not cloudid:
|
||||
print("Username not specified")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("Sharing with " + receiver)
|
||||
print("Sharing with " + cloudid)
|
||||
discovery_endpoint = uparse.scheme + "://" + \
|
||||
uparse.hostname + "/.well-known/ocm"
|
||||
req = requests.get(discovery_endpoint)
|
||||
|
@ -174,25 +190,8 @@ class OCM:
|
|||
print("Resource type " + resource_type + " is not supported")
|
||||
sys.exit(1)
|
||||
|
||||
provider_id = self.create_share(
|
||||
receiver, resource_type, file_or_folder)
|
||||
payload = self.get_share_payload(provider_id)
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
share_req = requests.post(
|
||||
share_endpoint,
|
||||
headers=headers,
|
||||
json=payload)
|
||||
if share_req.status_code != 200:
|
||||
print("Error: " + str(share_req.status_code))
|
||||
print(share_req.text)
|
||||
sys.exit(1)
|
||||
share_data = share_req.json()
|
||||
|
||||
print(share_data)
|
||||
self.create_share(
|
||||
cloudid, resource_type, file_or_folder, share_endpoint)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Reference in a new issue