diff --git a/base_directory_file_download/README.rst b/base_directory_file_download/README.rst new file mode 100644 index 000000000..bcf1941c8 --- /dev/null +++ b/base_directory_file_download/README.rst @@ -0,0 +1,78 @@ +.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: https://www.gnu.org/licenses/agpl + :alt: License: AGPL-3 + +======================== +Directory Files Download +======================== + +View and download the files contained in a directory on the server. + +This functionality can have impacts on the security of your system, +since it allows to download the content of a directory. +Be careful when choosing the directory! + +Notice that, for security reasons, files like symbolic links +and up-level references are ignored. + + +Configuration +============= + +To configure this module, you need to: + +#. Set the group "Download files of directory" for the users who need this functionality. + + +Usage +===== + +To use this module, you need to: + +#. Go to Settings -> Downloads -> Directory Content +#. Create a record specifying Name and Directory of the server +#. Save; a list of files contained in the selected directory is displayed +#. Download the file you need +#. In case the content of the directory is modified, refresh the list by clicking the button on the top-right of the form + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/149/10.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smash it by providing detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Andrea Stirpe + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/base_directory_file_download/__init__.py b/base_directory_file_download/__init__.py new file mode 100644 index 000000000..b44d76594 --- /dev/null +++ b/base_directory_file_download/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/base_directory_file_download/__manifest__.py b/base_directory_file_download/__manifest__.py new file mode 100644 index 000000000..03679b426 --- /dev/null +++ b/base_directory_file_download/__manifest__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Copyright 2017-2018 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + 'name': 'Directory Files Download', + 'summary': 'Download all files of a directory on server', + 'author': 'Onestein, Odoo Community Association (OCA)', + 'website': 'http://www.onestein.eu', + 'category': 'Tools', + 'version': '10.0.1.0.0', + 'license': 'AGPL-3', + 'depends': [ + 'base_setup', + ], + 'data': [ + 'security/groups.xml', + 'security/ir.model.access.csv', + 'views/ir_filesystem_directory.xml', + ], + 'installable': True, +} diff --git a/base_directory_file_download/models/__init__.py b/base_directory_file_download/models/__init__.py new file mode 100644 index 000000000..c88871f05 --- /dev/null +++ b/base_directory_file_download/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import ir_filesystem_directory +from . import ir_filesystem_file diff --git a/base_directory_file_download/models/ir_filesystem_directory.py b/base_directory_file_download/models/ir_filesystem_directory.py new file mode 100644 index 000000000..908e8a0cd --- /dev/null +++ b/base_directory_file_download/models/ir_filesystem_directory.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +# Copyright 2017-2018 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import logging + +from os import listdir +from os.path import isfile, join, exists, normpath, realpath +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + +_logger = logging.getLogger(__name__) + + +class IrFilesystemDirectory(models.Model): + _name = 'ir.filesystem.directory' + _description = 'Filesystem Directory' + + name = fields.Char(required=True, copy=False) + directory = fields.Char() + file_ids = fields.One2many( + 'ir.filesystem.file', + compute='_compute_file_ids', + string='Files' + ) + file_count = fields.Integer( + compute='_compute_file_count', + string="# Files" + ) + + @api.multi + def get_dir(self): + self.ensure_one() + directory = self.directory or '' + # adds slash character at the end if missing + return join(directory, '') + + @api.multi + def _compute_file_ids(self): + File = self.env['ir.filesystem.file'] + for directory in self: + directory.file_ids = None + if directory.get_dir(): + for file_name in directory._get_directory_files(): + directory.file_ids += File.create({ + 'name': file_name, + 'filename': file_name, + 'stored_filename': file_name, + 'directory_id': directory.id, + }) + + @api.onchange('directory') + def onchange_directory(self): + if self.directory and not exists(self.directory): + raise UserError(_('Directory does not exist')) + + @api.multi + def _compute_file_count(self): + for directory in self: + directory.file_count = len(directory.file_ids) + + @api.multi + def _get_directory_files(self): + + def get_files(directory, files): + for file_name in listdir(directory): + full_path = join(directory, file_name) + + # Symbolic links and up-level references are not considered + norm_path = normpath(realpath(full_path)) + if norm_path in full_path: + if isfile(full_path) and file_name[0] != '.': + files.append(file_name) + + self.ensure_one() + files = [] + if self.get_dir() and exists(self.get_dir()): + try: + get_files(self.get_dir(), files) + except (IOError, OSError): + _logger.info( + "_get_directory_files reading %s", + self.get_dir(), + exc_info=True + ) + return files + + @api.multi + def reload(self): + self.onchange_directory() + + @api.multi + def copy(self, default=None): + self.ensure_one() + default = dict(default or {}, name=_("%s (copy)") % self.name) + return super(IrFilesystemDirectory, self).copy(default=default) diff --git a/base_directory_file_download/models/ir_filesystem_file.py b/base_directory_file_download/models/ir_filesystem_file.py new file mode 100644 index 000000000..3c6d09d02 --- /dev/null +++ b/base_directory_file_download/models/ir_filesystem_file.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright 2017-2018 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import logging +import os + +from odoo import api, fields, models, _ +from odoo.exceptions import UserError +from odoo.tools import human_size + +_logger = logging.getLogger(__name__) + + +class IrFilesystemDirectoryLine(models.TransientModel): + _name = 'ir.filesystem.file' + + name = fields.Char(required=True) + filename = fields.Char() + file_content = fields.Binary(compute='_compute_file') + stored_filename = fields.Char() + directory_id = fields.Many2one( + 'ir.filesystem.directory', + string='Directory' + ) + + @api.multi + def _file_read(self, fname, bin_size=False): + + def file_not_found(fname): + raise UserError(_( + '''Error while reading file %s. + Maybe it was removed or permission is changed. + Please refresh the list.''' % fname)) + + self.ensure_one() + r = '' + directory = self.directory_id.get_dir() + full_path = directory + fname + if not (directory and os.path.isfile(full_path)): + file_not_found(fname) + try: + if bin_size: + r = human_size(os.path.getsize(full_path)) + else: + r = open(full_path, 'rb').read().encode('base64') + except (IOError, OSError): + _logger.info("_read_file reading %s", fname, exc_info=True) + return r + + @api.depends('stored_filename') + def _compute_file(self): + bin_size = self._context.get('bin_size') + for line in self: + if line.stored_filename: + content = line._file_read(line.stored_filename, bin_size) + line.file_content = content diff --git a/base_directory_file_download/security/groups.xml b/base_directory_file_download/security/groups.xml new file mode 100644 index 000000000..dd0b42604 --- /dev/null +++ b/base_directory_file_download/security/groups.xml @@ -0,0 +1,6 @@ + + + + Download files of directory + + diff --git a/base_directory_file_download/security/ir.model.access.csv b/base_directory_file_download/security/ir.model.access.csv new file mode 100644 index 000000000..ce3e8281f --- /dev/null +++ b/base_directory_file_download/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_ir_filesystem_directory,ir_filesystem_directory,model_ir_filesystem_directory,group_filesystem_directory,1,1,1,1 diff --git a/base_directory_file_download/tests/__init__.py b/base_directory_file_download/tests/__init__.py new file mode 100644 index 000000000..1375a4413 --- /dev/null +++ b/base_directory_file_download/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_directory_files_download diff --git a/base_directory_file_download/tests/test_directory_files_download.py b/base_directory_file_download/tests/test_directory_files_download.py new file mode 100644 index 000000000..8d15867a3 --- /dev/null +++ b/base_directory_file_download/tests/test_directory_files_download.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +# Copyright 2017-2018 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import os +from tempfile import gettempdir + +from odoo.tests import common +from odoo.exceptions import UserError + + +class TestBaseDirectoryFilesDownload(common.TransactionCase): + + def test_01_create(self): + test_dir = self.env['ir.filesystem.directory'].create({ + 'name': 'Test Directory 1', + 'directory': gettempdir() + }) + + # test method get_dir() + full_dir = test_dir.get_dir() + self.assertEqual(full_dir[-1], '/') + + # test computed field file_ids + self.assertGreaterEqual(len(test_dir.file_ids), 0) + + # test count list of directory + self.assertEqual(len(test_dir.file_ids), test_dir.file_count) + + # test reload list of directory + test_dir.reload() + self.assertEqual(len(test_dir.file_ids), test_dir.file_count) + + # test content of files + for file in test_dir.file_ids: + filename = file.stored_filename + directory = test_dir.get_dir() + with open(os.path.join(directory, filename), 'rb') as f: + content = f.read().encode('base64') + self.assertEqual(file.file_content, content) + + # test onchange directory (to not existing) + test_dir.directory = '/txxx' + with self.assertRaises(UserError): + test_dir.onchange_directory() + self.assertEqual(len(test_dir.file_ids), 0) + with self.assertRaises(UserError): + test_dir.reload() + self.assertEqual(len(test_dir.file_ids), 0) + + def test_02_copy(self): + test_dir = self.env['ir.filesystem.directory'].create({ + 'name': 'Test Orig', + 'directory': gettempdir() + }) + + # test copy + dir_copy = test_dir.copy() + self.assertEqual(dir_copy.name, 'Test Orig (copy)') + self.assertEqual(len(dir_copy.file_ids), test_dir.file_count) + self.assertEqual(dir_copy.file_count, test_dir.file_count) + + def test_03_not_existing_directory(self): + test_dir = self.env['ir.filesystem.directory'].create({ + 'name': 'Test Not Existing Directory', + 'directory': '/tpd' + }) + self.assertEqual(len(test_dir.file_ids), 0) + self.assertEqual(len(test_dir.file_ids), test_dir.file_count) + + # test onchange directory (to existing) + test_dir.directory = gettempdir() + self.assertGreaterEqual(len(test_dir.file_ids), 0) + self.assertEqual(len(test_dir.file_ids), test_dir.file_count) diff --git a/base_directory_file_download/views/ir_filesystem_directory.xml b/base_directory_file_download/views/ir_filesystem_directory.xml new file mode 100644 index 000000000..b1b2d42ac --- /dev/null +++ b/base_directory_file_download/views/ir_filesystem_directory.xml @@ -0,0 +1,75 @@ + + + + + ir.filesystem.directory + +
+ +
+ +
+

+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ + + ir.filesystem.directory + + + + + + + + + + Directory Content + ir.filesystem.directory + form + tree,form + + + + + + +