Commit af11b72e authored by Clemens Wolff's avatar Clemens Wolff

Enable edit-file for non-local file storages

parent 2bfb6b9c
......@@ -107,6 +107,20 @@ class LocalFileStorage(object):
"""
return send_file(file_path)
def read_file(self, path):
"""
Reads the content of the file located at `file_path`.
"""
with open(path, 'rb') as f:
return f.read()
def write_file(self, path, content):
"""
Writes `content` to the file located at `file_path`.
"""
with open(path, 'w') as f:
return f.write(content)
def save_file(self, path, file_data):
"""
Save uploaded file to the disk
......@@ -1118,8 +1132,7 @@ class BaseFileAdmin(BaseView, ActionsMixin):
form.process(request.form, content='')
if form.validate():
try:
with open(full_path, 'w') as f:
f.write(request.form['content'])
self.storage.write_file(full_path, request.form['content'])
except IOError:
flash(gettext("Error saving changes to %(name)s.", name=path), 'error')
error = True
......@@ -1131,8 +1144,7 @@ class BaseFileAdmin(BaseView, ActionsMixin):
helpers.flash_errors(form, message='Failed to edit file. %(error)s')
try:
with open(full_path, 'rb') as f:
content = f.read()
content = self.storage.read_file(full_path)
except IOError:
flash(gettext("Error reading %(name)s.", name=path), 'error')
error = True
......
......@@ -166,6 +166,14 @@ class S3Storage(object):
keys = self._get_path_keys(path + self.separator)
return len(keys) == 1
def read_file(self, path):
key = Key(self.bucket, path)
return key.get_contents_as_string()
def write_file(self, path, content):
key = Key(self.bucket, path)
key.set_contents_from_file(content)
class S3FileAdmin(BaseFileAdmin):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment