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 http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
from os import environ as env
|
from os import environ as env
|
||||||
from os import path, makedirs
|
from os import path, makedirs
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse, quote_plus
|
||||||
|
|
||||||
import pwd
|
import pwd
|
||||||
import requests
|
import requests
|
||||||
|
@ -61,7 +61,7 @@ class OCM:
|
||||||
if self.legacy_mode:
|
if self.legacy_mode:
|
||||||
return {
|
return {
|
||||||
"name": self.config[self.user][provider_id]["name"],
|
"name": self.config[self.user][provider_id]["name"],
|
||||||
"owner": self.user,
|
"owner": self.user + '@localhost.local',
|
||||||
"permission":
|
"permission":
|
||||||
self.config[self.user][provider_id]["permission"],
|
self.config[self.user][provider_id]["permission"],
|
||||||
"protocol": {
|
"protocol": {
|
||||||
|
@ -110,7 +110,7 @@ class OCM:
|
||||||
def receive(self):
|
def receive(self):
|
||||||
self.server.serve_forever()
|
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
|
provider_id = uuid.uuid4().hex
|
||||||
self.config[self.user][provider_id] = {
|
self.config[self.user][provider_id] = {
|
||||||
"name": name,
|
"name": name,
|
||||||
|
@ -121,17 +121,33 @@ class OCM:
|
||||||
"token": uuid.uuid4().hex,
|
"token": uuid.uuid4().hex,
|
||||||
}
|
}
|
||||||
self.write_config()
|
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):
|
def share(self, file, directory, to):
|
||||||
uparse = urlparse(to)
|
uparse = urlparse(to)
|
||||||
receiver = uparse.username
|
cloudid = f'{uparse.username}@{uparse.hostname}'
|
||||||
if not receiver:
|
if not cloudid:
|
||||||
print("Username not specified")
|
print("Username not specified")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
print("Sharing with " + receiver)
|
print("Sharing with " + cloudid)
|
||||||
discovery_endpoint = uparse.scheme + "://" + \
|
discovery_endpoint = uparse.scheme + "://" + \
|
||||||
uparse.hostname + "/.well-known/ocm"
|
uparse.hostname + "/.well-known/ocm"
|
||||||
req = requests.get(discovery_endpoint)
|
req = requests.get(discovery_endpoint)
|
||||||
|
@ -174,25 +190,8 @@ class OCM:
|
||||||
print("Resource type " + resource_type + " is not supported")
|
print("Resource type " + resource_type + " is not supported")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
provider_id = self.create_share(
|
self.create_share(
|
||||||
receiver, resource_type, file_or_folder)
|
cloudid, resource_type, file_or_folder, share_endpoint)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Reference in a new issue