diff --git a/base_directory_file_download/README.rst b/base_directory_file_download/README.rst new file mode 100644 index 000000000..2e81f4a4f --- /dev/null +++ b/base_directory_file_download/README.rst @@ -0,0 +1,98 @@ +======================== +Directory Files Download +======================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/11.0/base_directory_file_download + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-11-0/server-tools-11-0-base_directory_file_download + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/149/11.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +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. + +**Table of contents** + +.. contents:: + :local: + +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 + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Onestein + +Contributors +~~~~~~~~~~~~ + +* Andrea Stirpe + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_directory_file_download/__init__.py b/base_directory_file_download/__init__.py new file mode 100644 index 000000000..31660d6a9 --- /dev/null +++ b/base_directory_file_download/__init__.py @@ -0,0 +1,3 @@ +# 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..043eadc36 --- /dev/null +++ b/base_directory_file_download/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2017-2019 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': 'https://github.com/OCA/server-tools', + 'category': 'Tools', + 'version': '11.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..38cb57ad8 --- /dev/null +++ b/base_directory_file_download/models/__init__.py @@ -0,0 +1,4 @@ +# 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..cc8bcf75c --- /dev/null +++ b/base_directory_file_download/models/ir_filesystem_directory.py @@ -0,0 +1,98 @@ +# Copyright 2017-2019 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import logging + +from glob import glob +from os.path import basename, 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", + ) + files_filter = fields.Char() + + @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_filter): + glob_path = join(directory, files_filter) + content = glob(glob_path) + for full_path in content: + file_name = basename(full_path) + # 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(), self.files_filter or '*') + 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..e31d30c80 --- /dev/null +++ b/base_directory_file_download/models/ir_filesystem_file.py @@ -0,0 +1,58 @@ +# Copyright 2017-2019 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 +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' + _description = 'File in filesystem' + + 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 = base64.b64encode(open(full_path, 'rb').read()) + 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.env.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/readme/CONFIGURE.rst b/base_directory_file_download/readme/CONFIGURE.rst new file mode 100644 index 000000000..80d5c0dcb --- /dev/null +++ b/base_directory_file_download/readme/CONFIGURE.rst @@ -0,0 +1,3 @@ +To configure this module, you need to: + +#. Set the group "Download files of directory" for the users who need this functionality. diff --git a/base_directory_file_download/readme/CONTRIBUTORS.rst b/base_directory_file_download/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..4518218c8 --- /dev/null +++ b/base_directory_file_download/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Andrea Stirpe diff --git a/base_directory_file_download/readme/DESCRIPTION.rst b/base_directory_file_download/readme/DESCRIPTION.rst new file mode 100644 index 000000000..f350f09ba --- /dev/null +++ b/base_directory_file_download/readme/DESCRIPTION.rst @@ -0,0 +1,8 @@ +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. diff --git a/base_directory_file_download/readme/USAGE.rst b/base_directory_file_download/readme/USAGE.rst new file mode 100644 index 000000000..f15fdbc4c --- /dev/null +++ b/base_directory_file_download/readme/USAGE.rst @@ -0,0 +1,7 @@ +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 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/static/description/icon.png b/base_directory_file_download/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/base_directory_file_download/static/description/icon.png differ diff --git a/base_directory_file_download/static/description/index.html b/base_directory_file_download/static/description/index.html new file mode 100644 index 000000000..f0d68ce5a --- /dev/null +++ b/base_directory_file_download/static/description/index.html @@ -0,0 +1,444 @@ + + + + + + +Directory Files Download + + + +
+

Directory Files Download

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

+

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.

+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  1. Set the group “Download files of directory” for the users who need this functionality.
  2. +
+
+
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Settings -> Downloads -> Directory Content
  2. +
  3. Create a record specifying Name and Directory of the server
  4. +
  5. Save; a list of files contained in the selected directory is displayed
  6. +
  7. Download the file you need
  8. +
  9. In case the content of the directory is modified, refresh the list by clicking the button on the top-right of the form
  10. +
+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Onestein
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_directory_file_download/tests/__init__.py b/base_directory_file_download/tests/__init__.py new file mode 100644 index 000000000..b940b19c3 --- /dev/null +++ b/base_directory_file_download/tests/__init__.py @@ -0,0 +1,3 @@ +# 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..001392ef0 --- /dev/null +++ b/base_directory_file_download/tests/test_directory_files_download.py @@ -0,0 +1,74 @@ +# Copyright 2017-2019 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 +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 = base64.b64encode(f.read()) + 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..3b58983bb --- /dev/null +++ b/base_directory_file_download/views/ir_filesystem_directory.xml @@ -0,0 +1,76 @@ + + + + + ir.filesystem.directory + +
+ +
+ +
+

+ +

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