Skip to content

Commit

Permalink
Fix and harden WebDAV upload
Browse files Browse the repository at this point in the history
Remove unused port variable and GUI input.

Update urllib calls for new selective imports.

Always strip and re-add slashes from path elements so assure there is always exactly one slash between them.

Signed-off-by: MichaIng <[email protected]>
  • Loading branch information
MichaIng committed Apr 20, 2022
1 parent a5f6d2a commit 7f9113f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion motioneye/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
<td class="settings-item-value"><input type="text" class="styled storage camera-config" id="uploadServerEntry"></td>
<td><span class="help-mark" title="{{ _("la domajna nomo IP-adreso de la servilo (ekz. ftp.example.com 192.168.1.3)") }}">?</span></td>
</tr>
<tr class="settings-item" required="true" min="1" max="65535" depends="uploadEnabled uploadService=(ftp|sftp|http|https|webdav)">
<tr class="settings-item" required="true" min="1" max="65535" depends="uploadEnabled uploadService=(ftp|sftp|http|https)">
<td class="settings-item-label"><span class="settings-item-label">{{ _("Servila haveno") }}</span></td>
<td class="settings-item-value"><input type="text" class="number styled storage camera-config" id="uploadPortEntry"></td>
<td><span class="help-mark" title="{{ _("la haveno por uzi konekti al la servo (lasu ĉi tiun kampon malplena por uzi la defaŭltan valoron)") }}">?</span></td>
Expand Down
23 changes: 13 additions & 10 deletions motioneye/uploadservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ class Webdav(UploadService):

def __init__(self, camera_id):
self._server = None
self._port = None
self._username = None
self._password = None
self._location = None
Expand All @@ -902,11 +901,11 @@ def _request(self, url, method, body=None):
if body is not None:
headers.update({'Content-Length': len(body)})
self.debug(f'request: {method} {url}')
request = urllib.request.Request(url, data=body, headers=headers)
request = Request(url, data=body, headers=headers)
request.get_method = lambda: method
try:
utils.urlopen(request)
except urllib.error.HTTPError as e:
except HTTPError as e:
if method == 'MKCOL' and e.code == 405:
self.debug(
'MKCOL failed with code 405, this is normal if the folder exists'
Expand All @@ -915,27 +914,31 @@ def _request(self, url, method, body=None):
raise e

def _make_dirs(self, path):
dir_url = self._server
for folder in path.strip('/').split('/'):
dir_url = self._server.rstrip('/') + '/'
for folder in path.split('/'):
dir_url = dir_url + folder + '/'
self._request(dir_url, 'MKCOL')

def test_access(self):
try:
test_path = self._location.strip('/') + '/' + str(time.time())
self._make_dirs(test_path)
self._request(self._server + test_path, 'DELETE')
path = self._location.strip('/') + '/' + str(time.time())
self._make_dirs(path)
self._request(self._server.rstrip('/') + '/' + path, 'DELETE')
return True
except Exception as e:
self.error(str(e), exc_info=True)
return str(e)

def upload_data(self, filename, mime_type, data, ctime, camera_name):
path = self._location.strip('/') + '/' + os.path.dirname(filename) + '/'
path = self._location.strip('/') + '/' + os.path.dirname(filename)
filename = os.path.basename(filename)
self._make_dirs(path)
self.debug(f'uploading {filename} of {len(data)} bytes')
self._request(self._server + path + filename, 'PUT', bytearray(data))
self._request(
self._server.rstrip('/') + '/' + path + '/' + filename,
'PUT',
bytearray(data),
)
self.debug('upload done')

def dump(self):
Expand Down

0 comments on commit 7f9113f

Please sign in to comment.