-
-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathlocal.py
More file actions
84 lines (69 loc) · 2.86 KB
/
Copy pathlocal.py
File metadata and controls
84 lines (69 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import io
import os
from django.conf import settings
from django.utils import timezone
from os import listdir
from os.path import isfile, join
from cookbook.models import Recipe, RecipeImport, SyncLog
from cookbook.provider.provider import Provider
class Local(Provider):
@staticmethod
def import_all(monitor):
if not Local.is_path_allowed(monitor.path):
log_entry = SyncLog(
status='ERROR',
msg='Path not allowed',
sync=monitor,
)
log_entry.save()
return log_entry
files = [f for f in listdir(monitor.path) if isfile(join(monitor.path, f))]
import_count = 0
for file in files:
if file.endswith('.pdf') or file.endswith('.png') or file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.gif'):
path = monitor.path + '/' + file
if not Recipe.objects.filter(file_path__iexact=path, space=monitor.space).exists() and not RecipeImport.objects.filter(file_path=path, space=monitor.space).exists():
name = os.path.splitext(file)[0]
new_recipe = RecipeImport(
name=name,
file_path=path,
storage=monitor.storage,
space=monitor.space,
)
new_recipe.save()
import_count += 1
log_entry = SyncLog(
status='SUCCESS',
msg='Imported ' + str(import_count) + ' recipes',
sync=monitor,
)
log_entry.save()
monitor.last_checked = timezone.now()
monitor.save()
return log_entry
@staticmethod
def get_file(recipe):
if not Local.is_path_allowed(recipe.file_path):
raise Exception('Path not allowed')
file = io.BytesIO(open(recipe.file_path, 'rb').read())
return file
@staticmethod
def is_path_allowed(path):
normalized_path = os.path.normpath(os.path.abspath(path))
for allowed_path in settings.LOCAL_STORAGE_PATHS:
normalized_allowed_path = os.path.normpath(os.path.abspath(allowed_path))
if normalized_path.startswith(normalized_allowed_path + os.sep) or normalized_path == normalized_allowed_path:
return True
return False
@staticmethod
def rename_file(recipe, new_name):
if not Local.is_path_allowed(recipe.file_path):
raise Exception('Path not allowed')
os.rename(recipe.file_path, os.path.join(os.path.dirname(recipe.file_path), (new_name + os.path.splitext(recipe.file_path)[1])))
return True
@staticmethod
def delete_file(recipe):
if not Local.is_path_allowed(recipe.file_path):
raise Exception('Path not allowed')
os.remove(recipe.file_path)
return True