provider/nextcloud
This commit is contained in:
parent
41c8e53569
commit
f7ff700c7a
@ -1,15 +1,13 @@
|
|||||||
import base64
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import webdav3.client as wc
|
|
||||||
import requests
|
|
||||||
from io import BytesIO
|
|
||||||
from requests.auth import HTTPBasicAuth
|
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import webdav3.client as wc
|
||||||
from cookbook.models import Recipe, RecipeImport, SyncLog
|
from cookbook.models import Recipe, RecipeImport, SyncLog
|
||||||
from cookbook.provider.provider import Provider
|
from cookbook.provider.provider import Provider
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
|
|
||||||
class Nextcloud(Provider):
|
class Nextcloud(Provider):
|
||||||
@ -34,14 +32,22 @@ class Nextcloud(Provider):
|
|||||||
import_count = 0
|
import_count = 0
|
||||||
for file in files:
|
for file in files:
|
||||||
path = monitor.path + '/' + file
|
path = monitor.path + '/' + file
|
||||||
if not Recipe.objects.filter(file_path__iexact=path).exists() and not RecipeImport.objects.filter(
|
if not Recipe.objects.filter(file_path__iexact=path).exists() \
|
||||||
file_path=path).exists():
|
and not RecipeImport.objects.filter(file_path=path).exists(): # noqa: E501
|
||||||
name = os.path.splitext(file)[0]
|
name = os.path.splitext(file)[0]
|
||||||
new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage)
|
new_recipe = RecipeImport(
|
||||||
|
name=name,
|
||||||
|
file_path=path,
|
||||||
|
storage=monitor.storage
|
||||||
|
)
|
||||||
new_recipe.save()
|
new_recipe.save()
|
||||||
import_count += 1
|
import_count += 1
|
||||||
|
|
||||||
log_entry = SyncLog(status='SUCCESS', msg='Imported ' + str(import_count) + ' recipes', sync=monitor)
|
log_entry = SyncLog(
|
||||||
|
status='SUCCESS',
|
||||||
|
msg='Imported ' + str(import_count) + ' recipes',
|
||||||
|
sync=monitor
|
||||||
|
)
|
||||||
log_entry.save()
|
log_entry.save()
|
||||||
|
|
||||||
monitor.last_checked = datetime.now()
|
monitor.last_checked = datetime.now()
|
||||||
@ -51,7 +57,7 @@ class Nextcloud(Provider):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_share_link(recipe):
|
def create_share_link(recipe):
|
||||||
url = recipe.storage.url + '/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json'
|
url = recipe.storage.url + '/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json' # noqa: E501
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"OCS-APIRequest": "true",
|
"OCS-APIRequest": "true",
|
||||||
@ -60,8 +66,14 @@ class Nextcloud(Provider):
|
|||||||
|
|
||||||
data = {'path': recipe.file_path, 'shareType': 3}
|
data = {'path': recipe.file_path, 'shareType': 3}
|
||||||
|
|
||||||
r = requests.post(url, headers=headers, auth=HTTPBasicAuth(recipe.storage.username, recipe.storage.password),
|
r = requests.post(
|
||||||
data=data)
|
url,
|
||||||
|
headers=headers,
|
||||||
|
auth=HTTPBasicAuth(
|
||||||
|
recipe.storage.username, recipe.storage.password
|
||||||
|
),
|
||||||
|
data=data
|
||||||
|
)
|
||||||
|
|
||||||
response_json = r.json()
|
response_json = r.json()
|
||||||
|
|
||||||
@ -69,14 +81,20 @@ class Nextcloud(Provider):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_share_link(recipe):
|
def get_share_link(recipe):
|
||||||
url = recipe.storage.url + '/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json&path=' + recipe.file_path
|
url = recipe.storage.url + '/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json&path=' + recipe.file_path # noqa: E501
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"OCS-APIRequest": "true",
|
"OCS-APIRequest": "true",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
r = requests.get(url, headers=headers, auth=HTTPBasicAuth(recipe.storage.username, recipe.storage.password))
|
r = requests.get(
|
||||||
|
url,
|
||||||
|
headers=headers,
|
||||||
|
auth=HTTPBasicAuth(
|
||||||
|
recipe.storage.username, recipe.storage.password
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
response_json = r.json()
|
response_json = r.json()
|
||||||
for element in response_json['ocs']['data']:
|
for element in response_json['ocs']['data']:
|
||||||
@ -91,7 +109,10 @@ class Nextcloud(Provider):
|
|||||||
|
|
||||||
tmp_file_path = tempfile.gettempdir() + '/' + recipe.name + '.pdf'
|
tmp_file_path = tempfile.gettempdir() + '/' + recipe.name + '.pdf'
|
||||||
|
|
||||||
client.download_file(remote_path=recipe.file_path, local_path=tmp_file_path)
|
client.download_file(
|
||||||
|
remote_path=recipe.file_path,
|
||||||
|
local_path=tmp_file_path
|
||||||
|
)
|
||||||
|
|
||||||
file = io.BytesIO(open(tmp_file_path, 'rb').read())
|
file = io.BytesIO(open(tmp_file_path, 'rb').read())
|
||||||
os.remove(tmp_file_path)
|
os.remove(tmp_file_path)
|
||||||
@ -102,8 +123,14 @@ class Nextcloud(Provider):
|
|||||||
def rename_file(recipe, new_name):
|
def rename_file(recipe, new_name):
|
||||||
client = Nextcloud.get_client(recipe.storage)
|
client = Nextcloud.get_client(recipe.storage)
|
||||||
|
|
||||||
client.move(recipe.file_path,
|
client.move(
|
||||||
os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1])
|
recipe.file_path,
|
||||||
|
"%s/%s%s" % (
|
||||||
|
os.path.dirname(recipe.file_path),
|
||||||
|
new_name,
|
||||||
|
os.path.splitext(recipe.file_path)[1]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user