Skip to content

Commit

Permalink
Merge PR #407 into 18.0
Browse files Browse the repository at this point in the history
Signed-off-by lmignon
  • Loading branch information
OCA-git-bot committed Nov 10, 2024
2 parents a75ee69 + 5649725 commit a1758f6
Show file tree
Hide file tree
Showing 26 changed files with 1,576 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .oca/oca-port/blacklist/storage_backend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"pull_requests": {
"orphaned_commits": "false-positive",
"78": "false-positive",
"97": "false-positive",
"106": "false-positive",
"298": "false-positive"
}
}
100 changes: 100 additions & 0 deletions storage_backend/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
===============
Storage Backend
===============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:e7d57aec1028f8dcc4fc465d14076ef3f8d791eb5425d23fb5d3fb4ef30c565b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstorage-lightgray.png?logo=github
:target: https://github.com/OCA/storage/tree/18.0/storage_backend
:alt: OCA/storage
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/storage-18-0/storage-18-0-storage_backend
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/storage&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|



**Table of contents**

.. contents::
:local:

Usage
=====



Changelog
=========



Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/storage/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/storage/issues/new?body=module:%20storage_backend%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
-------

* Akretion

Contributors
------------

- Sébastien BEAU <[email protected]>
- Raphaël Reverdy <[email protected]>
- Florian da Costa <[email protected]>
- Cédric Pigeon <[email protected]>
- Renato Lima <[email protected]>
- Benoît Guillot <[email protected]>
- Laurent Mignon <[email protected]>
- Denis Roussel <[email protected]>
- Thien Vo <[email protected]>

Other credits
-------------

The migration of this module from 16.0 to 18.0 was financially supported
by Camptocamp.

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/storage <https://github.com/OCA/storage/tree/18.0/storage_backend>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions storage_backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import components
21 changes: 21 additions & 0 deletions storage_backend/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "Storage Backend",
"summary": "Implement the concept of Storage with amazon S3, sftp...",
"version": "18.0.1.0.0",
"category": "Storage",
"website": "https://github.com/OCA/storage",
"author": " Akretion, Odoo Community Association (OCA)",
"license": "LGPL-3",
"development_status": "Production/Stable",
"installable": True,
"depends": ["base", "component", "server_environment"],
"data": [
"views/backend_storage_view.xml",
"data/data.xml",
"security/ir.model.access.csv",
],
}
2 changes: 2 additions & 0 deletions storage_backend/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import base_adapter
from . import filesystem_adapter
69 changes: 69 additions & 0 deletions storage_backend/components/base_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2017 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
# Copyright 2020 ACSONE SA/NV (<http://acsone.eu>)
# @author Simone Orsi <[email protected]>

import os
import re

from odoo.addons.component.core import AbstractComponent


class BaseStorageAdapter(AbstractComponent):
_name = "base.storage.adapter"
_collection = "storage.backend"

def _fullpath(self, relative_path):
dp = self.collection.directory_path
if not dp or relative_path.startswith(dp):
return relative_path
return os.path.join(dp, relative_path)

def add(self, relative_path, data, **kwargs):
raise NotImplementedError

def get(self, relative_path, **kwargs):
raise NotImplementedError

def list(self, relative_path=""):
raise NotImplementedError

def find_files(self, pattern, relative_path="", **kwargs):
"""Find files matching given pattern.
:param pattern: regex expression
:param relative_path: optional relative path containing files
:return: list of file paths as full paths from the root
"""
regex = re.compile(pattern)
filelist = self.list(relative_path)
files_matching = [
regex.match(file_).group() for file_ in filelist if regex.match(file_)
]
filepaths = []
if files_matching:
filepaths = [
os.path.join(self._fullpath(relative_path) or "", filename)
for filename in files_matching
]
return filepaths

def move_files(self, files, destination_path, **kwargs):
"""Move files to given destination.
:param files: list of file paths to be moved
:param destination_path: directory path where to move files
:return: None
"""
raise NotImplementedError

def delete(self, relative_path):
raise NotImplementedError

# You can define `validate_config` on your own adapter
# to make validation button available on UI.
# This method should simply pass smoothly when validation is ok,
# otherwise it should raise an exception.
# def validate_config(self):
# raise NotImplementedError
75 changes: 75 additions & 0 deletions storage_backend/components/filesystem_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2017 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
import os
import shutil

from odoo.exceptions import AccessError

from odoo.addons.component.core import Component

_logger = logging.getLogger(__name__)


def is_safe_path(basedir, path):
return os.path.realpath(path).startswith(basedir)


class FileSystemStorageBackend(Component):
_name = "filesystem.adapter"
_inherit = "base.storage.adapter"
_usage = "filesystem"

def _basedir(self):
return os.path.join(self.env["ir.attachment"]._filestore(), "storage")

def _fullpath(self, relative_path):
"""This will build the full path for the file, we force to
store the data inside the filestore in the directory 'storage".
Becarefull if you implement your own custom path, end user
should never be able to write or read unwanted filesystem file"""
full_path = super()._fullpath(relative_path)
base_dir = self._basedir()
full_path = os.path.join(base_dir, full_path)
if not is_safe_path(base_dir, full_path):
raise AccessError(self.env._("Access to %s is forbidden") % full_path)
return full_path

def add(self, relative_path, data, **kwargs):
full_path = self._fullpath(relative_path)
dirname = os.path.dirname(full_path)
if not os.path.isdir(dirname):
os.makedirs(dirname)
with open(full_path, "wb") as my_file:
my_file.write(data)

def get(self, relative_path, **kwargs):
full_path = self._fullpath(relative_path)
with open(full_path, "rb") as my_file:
data = my_file.read()
return data

def list(self, relative_path=""):
full_path = self._fullpath(relative_path)
if os.path.isdir(full_path):
return os.listdir(full_path)
return []

def delete(self, relative_path):
full_path = self._fullpath(relative_path)
try:
os.remove(full_path)
except FileNotFoundError:
_logger.warning("File not found in %s", full_path)

def move_files(self, files, destination_path):
result = []
for file_path in files:
if not os.path.exists(destination_path):
os.makedirs(destination_path)
filename = os.path.basename(file_path)
destination_file = os.path.join(destination_path, filename)
result.append(shutil.move(file_path, destination_file))
return result
7 changes: 7 additions & 0 deletions storage_backend/data/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="default_storage_backend" model="storage.backend">
<field name="name">Filesystem Backend</field>
<field name="backend_type">filesystem</field>
</record>
</odoo>
Loading

0 comments on commit a1758f6

Please sign in to comment.