diff --git a/document_sftp/document_sftp_sftp_server.py b/document_sftp/document_sftp_sftp_server.py index 09f36fab..d394ed73 100644 --- a/document_sftp/document_sftp_sftp_server.py +++ b/document_sftp/document_sftp_sftp_server.py @@ -37,6 +37,13 @@ class DocumentSFTPSftpServerInterface(SFTPServerInterface): return SFTP_PERMISSION_DENIED return handler._open(path, flags, attr) + def remove(self, path): + handler = self.env['document.sftp']._get_handler_for(path) + if handler is None: + return SFTP_PERMISSION_DENIED + return handler._remove(path) + + def session_ended(self): self.env.cr.commit() self.env.cr.close() diff --git a/document_sftp/models/document_sftp_root.py b/document_sftp/models/document_sftp_root.py index c3e80629..a9b3d834 100644 --- a/document_sftp/models/document_sftp_root.py +++ b/document_sftp/models/document_sftp_root.py @@ -62,6 +62,11 @@ class DocumentSFTPRoot(models.AbstractModel): """Return file attributes""" raise NotImplementedError() + @api.model + def _remove(self, path): + """Return file attributes""" + raise NotImplementedError() + @api.model def _lstat(self, path): """Return attributes about a link""" diff --git a/document_sftp/models/document_sftp_root_by_model.py b/document_sftp/models/document_sftp_root_by_model.py index e9bf468c..1cc80895 100644 --- a/document_sftp/models/document_sftp_root_by_model.py +++ b/document_sftp/models/document_sftp_root_by_model.py @@ -2,9 +2,9 @@ # © 2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import os -from openerp import api, models +from openerp import api, models, exceptions try: - from paramiko import SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED + from paramiko import SFTP_OK, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED except ImportError: # pragma: no cover pass @@ -19,6 +19,18 @@ class DocumentSFTPRootByModel(models.Model): def _get_root_attributes(self): return self._directory(self._virtual_root) + @api.model + def _get_attachment(self, components): + """return an attachment if we have a full path + in the form /By model/res.company/$id/$attachment""" + return self.env['ir.attachment'].search([ + ('res_model', '=', components[-3]), + ('res_id', '=', components[-2]), + '|', + ('datas_fname', '=', components[-1]), + ('name', '=', components[-1]), + ], limit=1) + @api.model def _stat(self, path): path = path.strip('/') @@ -30,13 +42,7 @@ class DocumentSFTPRootByModel(models.Model): elif len(components) in (2, 3): return self._directory(components[-1]) elif len(components) == 4: - return self._file(self.env['ir.attachment'].search([ - ('res_model', '=', components[-3]), - ('res_id', '=', components[-2]), - '|', - ('datas_fname', '=', components[-1]), - ('name', '=', components[-1]), - ], limit=1)) + return self._file(self._get_attachment(components)) return SFTP_NO_SUCH_FILE @api.model @@ -82,11 +88,7 @@ class DocumentSFTPRootByModel(models.Model): components = self._split_path(path) if len(components) == 4: # TODO: locking! - existing = self.env['ir.attachment'].search([ - ('res_model', '=', components[-3]), - ('res_id', '=', int(components[-2])), - ('datas_fname', '=', components[-1]), - ]) + existing = self._get_attachment(components) return self._file_handle( existing or self.env['ir.attachment'].new({ 'res_model': components[-3], @@ -101,11 +103,16 @@ class DocumentSFTPRootByModel(models.Model): def _open_read(self, path, flags, attr): components = self._split_path(path) if len(components) == 4: - return self._file_handle(self.env['ir.attachment'].search([ - ('res_model', '=', components[-3]), - ('res_id', '=', components[-2]), - '|', - ('datas_fname', '=', components[-1]), - ('name', '=', components[-1]), - ], limit=1)) + return self._file_handle(self._get_attachment(components)) return SFTP_PERMISSION_DENIED + + @api.model + def _remove(self, path): + components = self._split_path(path) + if len(components) == 4: + try: + self._get_attachment(components).unlink() + self.env.cr.commit() + except exceptions.AccessError: + return SFTP_PERMISSION_DENIED + return SFTP_OK