Нет описания

ImageHandler.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # contact@dfir-iris.org
  4. # Created by Lukas Zurschmiede @LukyLuke
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 3 of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License
  17. # along with this program; if not, write to the Free Software Foundation,
  18. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. import logging
  20. import os
  21. import shutil
  22. import uuid
  23. import re
  24. from pathlib import Path
  25. from docxtpl import DocxTemplate
  26. from docx_generator.globals.picture_globals import PictureGlobals
  27. from app.datamgmt.datastore.datastore_db import datastore_get_local_file_path
  28. class ImageHandler(PictureGlobals):
  29. def __init__(self, template: DocxTemplate, base_path: str):
  30. self._logger = logging.getLogger(__name__)
  31. PictureGlobals.__init__(self, template, base_path)
  32. def _process_remote(self, image_path: str) -> str:
  33. """
  34. Checks if the given Link is a datastore-link and if so, save the image locally for further processing.
  35. :
  36. A Datastore Links looks like this: https://localhost:4433/datastore/file/view/2?cid=1
  37. """
  38. res = re.search(r'datastore\/file\/view\/(\d+)\?cid=(\d+)', image_path)
  39. if not res:
  40. return super()._process_remote(image_path)
  41. if image_path[:4] == 'http' and len(res.groups()) == 2:
  42. file_id = res.groups(0)[0]
  43. case_id = res.groups(0)[1]
  44. has_error, dsf = datastore_get_local_file_path(file_id, case_id)
  45. if has_error:
  46. raise RenderingError(self._logger, f'File-ID {file_id} does not exist in Case {case_id}')
  47. if not Path(dsf.file_local_name).is_file():
  48. raise RenderingError(self._logger, f'File {dsf.file_local_name} does not exists on the server. Update or delete virtual entry')
  49. file_ext = os.path.splitext(dsf.file_original_name)[1]
  50. file_name = os.path.join(self._output_path, str(uuid.uuid4())) + file_ext
  51. return_value = shutil.copy(dsf.file_local_name, file_name)
  52. return return_value
  53. return super()._process_remote(image_path)