|
@@ -2,11 +2,14 @@ from .context import context_adaption
|
2
|
2
|
from django.conf import settings
|
3
|
3
|
from django.shortcuts import render, redirect, HttpResponse
|
4
|
4
|
from django.http import HttpResponseNotFound, HttpResponseForbidden
|
|
5
|
+import fstools
|
5
|
6
|
import importlib
|
6
|
7
|
import mimetypes
|
|
8
|
+from mycreole import url_upload, url_attachment, url_delete
|
7
|
9
|
import logging
|
8
|
10
|
import mycreole
|
9
|
11
|
import os
|
|
12
|
+import stringtools
|
10
|
13
|
import themes
|
11
|
14
|
from django.contrib import messages
|
12
|
15
|
|
|
@@ -26,6 +29,13 @@ def get_method(import_string):
|
26
|
29
|
return getattr(module, class_str)
|
27
|
30
|
|
28
|
31
|
|
|
32
|
+def get_next(request):
|
|
33
|
+ if not request.POST:
|
|
34
|
+ return request.GET.get('next', '/')
|
|
35
|
+ else:
|
|
36
|
+ return request.POST.get('next', '/')
|
|
37
|
+
|
|
38
|
+
|
29
|
39
|
def access(access_type, request, rel_path):
|
30
|
40
|
def log_warning(access_type, e):
|
31
|
41
|
logger.warning('Could not import %s_access method for checking access rights: %s - %s', access_type, type(e).__name__, e)
|
|
@@ -66,10 +76,20 @@ def mycreole_upload(request, rel_path):
|
66
|
76
|
if access('modify', request, rel_path):
|
67
|
77
|
if not request.POST:
|
68
|
78
|
context = themes.Context(request)
|
69
|
|
- context_adaption(context, request, 'Upload %s' % rel_path, next=request.GET.get('next', '/'))
|
|
79
|
+ context_adaption(
|
|
80
|
+ context,
|
|
81
|
+ request,
|
|
82
|
+ 'Upload %s' % rel_path,
|
|
83
|
+ os.path.dirname(rel_path),
|
|
84
|
+ rel_path_is_directory=os.path.isdir(mycreole.get_full_path(rel_path)),
|
|
85
|
+ next=request.GET.get('next', '/')
|
|
86
|
+ )
|
70
|
87
|
return render(request, 'mycreole/upload.html', context=context)
|
71
|
88
|
else:
|
|
89
|
+ filename = request.POST.get('filename')
|
72
|
90
|
full_path = mycreole.get_full_path(rel_path)
|
|
91
|
+ if filename is not None:
|
|
92
|
+ full_path = os.path.join(full_path, filename)
|
73
|
93
|
try:
|
74
|
94
|
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
75
|
95
|
except PermissionError:
|
|
@@ -77,12 +97,81 @@ def mycreole_upload(request, rel_path):
|
77
|
97
|
else:
|
78
|
98
|
with open(full_path, 'wb') as fh:
|
79
|
99
|
fh.write(request.FILES['file'].read())
|
|
100
|
+ messages.info(request, 'File %s successfully uploaded.' % os.path.basename(full_path))
|
80
|
101
|
return redirect(request.POST.get('next', '/'))
|
81
|
102
|
else:
|
82
|
103
|
messages.error(request, "Upload: Access denied!")
|
83
|
104
|
return redirect(request.GET.get('next', '/'))
|
84
|
105
|
|
85
|
106
|
|
|
107
|
+def mycreole_delete(request, rel_path):
|
|
108
|
+ context = themes.Context(request) # needs to be executed first because of time mesurement
|
|
109
|
+ nxt = get_next(request)
|
|
110
|
+ modify_access = access('modify', request, rel_path)
|
|
111
|
+ full_path = mycreole.get_full_path(rel_path)
|
|
112
|
+ if not modify_access:
|
|
113
|
+ messages.error(request, 'Detele Attachment: Access denied!')
|
|
114
|
+ else:
|
|
115
|
+ if not request.POST:
|
|
116
|
+ context_adaption(
|
|
117
|
+ context,
|
|
118
|
+ request,
|
|
119
|
+ 'Delete %s' % os.path.basename(rel_path),
|
|
120
|
+ os.path.dirname(rel_path),
|
|
121
|
+ next=nxt,
|
|
122
|
+ filename=os.path.basename(rel_path)
|
|
123
|
+ )
|
|
124
|
+ return render(request, 'mycreole/delete.html', context=context)
|
|
125
|
+ else:
|
|
126
|
+ if request.POST.get('yes') is not None:
|
|
127
|
+ try:
|
|
128
|
+ os.remove(full_path)
|
|
129
|
+ except Exception:
|
|
130
|
+ messages.error(request, 'Error while deleting file from filesystem.')
|
|
131
|
+ logger.exception('Fatal error while deleting Attachment!')
|
|
132
|
+ else:
|
|
133
|
+ messages.info(request, "Attachment %s deleted..." % os.path.basename(full_path))
|
|
134
|
+ else:
|
|
135
|
+ messages.info(request, 'Delete request aborded.')
|
|
136
|
+ return redirect(nxt)
|
|
137
|
+
|
|
138
|
+
|
86
|
139
|
def mycreole_manageuploads(request, rel_path):
|
87
|
|
- messages.error(request, 'Manage Uploads: Not yet implemented!')
|
88
|
|
- return redirect(request.GET.get('next', '/'))
|
|
140
|
+ context = themes.Context(request) # needs to be executed first because of time mesurement
|
|
141
|
+ nxt = get_next(request)
|
|
142
|
+ read_access = access('read', request, rel_path)
|
|
143
|
+ modify_access = access('modify', request, rel_path)
|
|
144
|
+ #
|
|
145
|
+ if not read_access:
|
|
146
|
+ messages.error(request, "Manageupload: Access denied!")
|
|
147
|
+ return redirect(nxt)
|
|
148
|
+
|
|
149
|
+ basepath = mycreole.get_full_path(rel_path)
|
|
150
|
+ if not os.path.exists(basepath):
|
|
151
|
+ logger.info('Creating path for attachments: %s', basepath)
|
|
152
|
+ fstools.mkdir(basepath)
|
|
153
|
+ logger.debug("Searching for files in %s", basepath)
|
|
154
|
+ attachments = {}
|
|
155
|
+ for file in fstools.filelist(basepath):
|
|
156
|
+ filename = os.path.basename(file)
|
|
157
|
+ actions = []
|
|
158
|
+ if modify_access:
|
|
159
|
+ actions.append((themes.gray_icon_url(request, 'delete.png'), url_delete(request, os.path.join(rel_path, filename)), 'delete'))
|
|
160
|
+ actions.append((themes.gray_icon_url(request, 'shuffle.png'), url_upload(request, os.path.join(rel_path, filename)), 'replace'))
|
|
161
|
+ actions.append((themes.gray_icon_url(request, 'view.png'), url_attachment(os.path.join(rel_path, filename)), 'view'))
|
|
162
|
+ attachments[filename] = {
|
|
163
|
+ 'size': stringtools.physical_value_repr(os.path.getsize(file), 'B'),
|
|
164
|
+ 'modify_access': modify_access,
|
|
165
|
+ 'actions': actions,
|
|
166
|
+ }
|
|
167
|
+ logger.debug("%d attachments detected: %s", len(attachments), ', '.join(attachments.keys()))
|
|
168
|
+ #
|
|
169
|
+ mycreole.context.context_adaption(
|
|
170
|
+ context,
|
|
171
|
+ request,
|
|
172
|
+ 'Manageuploads for %s' % rel_path,
|
|
173
|
+ rel_path,
|
|
174
|
+ next=nxt,
|
|
175
|
+ attachments=attachments
|
|
176
|
+ )
|
|
177
|
+ return render(request, 'mycreole/manageuploads.html', context=context)
|