Ticket #888: 0001-Implement-Raw-Image-media-type-plugin.patch

File 0001-Implement-Raw-Image-media-type-plugin.patch, 8.7 KB (added by Odin Hørthe Omdal (Velmont), 10 years ago)

Implement Raw Image media type

  • docs/source/siteadmin/media-types.rst

    From de674dae356b7a2c1a9c8eed8063173debad5148 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Odin=20H=C3=B8rthe=20Omdal?= <odinho@opera.com>
    Date: Mon, 12 May 2014 23:41:03 +0200
    Subject: [PATCH] Implement Raw Image media type plugin
    
    ---
     docs/source/siteadmin/media-types.rst           | 28 ++++++++-
     mediagoblin/media_types/image/__init__.py       |  2 -
     mediagoblin/media_types/raw_image/__init__.py   | 37 +++++++++++
     mediagoblin/media_types/raw_image/models.py     | 21 +++++++
     mediagoblin/media_types/raw_image/processing.py | 82 +++++++++++++++++++++++++
     5 files changed, 165 insertions(+), 5 deletions(-)
     create mode 100644 mediagoblin/media_types/raw_image/__init__.py
     create mode 100644 mediagoblin/media_types/raw_image/models.py
     create mode 100644 mediagoblin/media_types/raw_image/processing.py
    
    diff --git a/docs/source/siteadmin/media-types.rst b/docs/source/siteadmin/media-types.rst
    index 3e8a94e..44ad02b 100644
    a b  
    11.. MediaGoblin Documentation
    22
    3    Written in 2011, 2012 by MediaGoblin contributors
     3   Written in 2011, 2012, 2014 by MediaGoblin contributors
    44
    55   To the extent possible under law, the author(s) have dedicated all
    66   copyright and related and neighboring rights to this software to
    Media Types  
    1818====================
    1919
    2020In the future, there will be all sorts of media types you can enable,
    21 but in the meanwhile there are five additional media types: video, audio,
    22 ascii art, STL/3d models, PDF and Document.
     21but in the meanwhile there are six additional media types: video, audio,
     22raw image, ascii art, STL/3d models, PDF and Document.
    2323
    2424First, you should probably read ":doc:`configuration`" to make sure
    2525you know how to modify the mediagoblin config file.
    Run  
    149149You should now be able to upload and listen to audio files!
    150150
    151151
     152Raw image
     153=========
     154
     155To enable raw image you need to install pyexiv2.  On Debianoid systems
     156
     157.. code-block:: bash
     158
     159    sudo apt-get install python-pyexiv2
     160
     161Add ``[[mediagoblin.media_types.raw_image]]`` under the ``[plugins]``
     162section in your ``mediagoblin_local.ini`` and restart MediaGoblin.
     163
     164Run
     165
     166.. code-block:: bash
     167
     168    ./bin/gmg dbupdate
     169
     170Now you should be able to submit raw images, and mediagoblin should
     171extract the JPEG preview from them.
     172
     173
    152174Ascii art
    153175=========
    154176
  • mediagoblin/media_types/image/__init__.py

    diff --git a/mediagoblin/media_types/image/__init__.py b/mediagoblin/media_types/image/__init__.py
    index f5b49f0..06e0f08 100644
    a b _log = logging.getLogger(__name__)  
    2727ACCEPTED_EXTENSIONS = ["jpg", "jpeg", "png", "gif", "tiff"]
    2828MEDIA_TYPE = 'mediagoblin.media_types.image'
    2929
    30 def setup_plugin():
    31     config = pluginapi.get_config(MEDIA_TYPE)
    3230
    3331class ImageMediaManager(MediaManagerBase):
    3432    human_readable = "Image"
  • new file mediagoblin/media_types/raw_image/__init__.py

    diff --git a/mediagoblin/media_types/raw_image/__init__.py b/mediagoblin/media_types/raw_image/__init__.py
    new file mode 100644
    index 0000000..046a9b2
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2014 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17from mediagoblin.media_types.image import ImageMediaManager
     18from mediagoblin.media_types.raw_image.processing import (
     19    ACCEPTED_EXTENSIONS, MEDIA_TYPE,
     20    RawImageProcessingManager, sniff_handler)
     21
     22
     23class RawImageMediaManager(ImageMediaManager):
     24    human_readable = "Raw image"
     25
     26
     27def get_media_type_and_manager(ext):
     28    if ext in ACCEPTED_EXTENSIONS:
     29        return MEDIA_TYPE, RawImageMediaManager
     30
     31
     32hooks = {
     33    'get_media_type_and_manager': get_media_type_and_manager,
     34    'sniff_handler': sniff_handler,
     35    ('media_manager', MEDIA_TYPE): lambda: RawImageMediaManager,
     36    ('reprocess_manager', MEDIA_TYPE): lambda: RawImageProcessingManager,
     37}
  • new file mediagoblin/media_types/raw_image/models.py

    diff --git a/mediagoblin/media_types/raw_image/models.py b/mediagoblin/media_types/raw_image/models.py
    new file mode 100644
    index 0000000..d3d68b9
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2014 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17from mediagoblin.media_types.image.models import (
     18    BACKREF_NAME, DATA_MODEL)
     19
     20
     21MODELS = None
  • new file mediagoblin/media_types/raw_image/processing.py

    diff --git a/mediagoblin/media_types/raw_image/processing.py b/mediagoblin/media_types/raw_image/processing.py
    new file mode 100644
    index 0000000..83b0155
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2014 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17import os
     18import logging
     19
     20# This needs to handle the case where it's missing
     21import pyexiv2
     22
     23from mediagoblin.media_types.image.processing import (
     24    InitialProcessor, Resizer)
     25from mediagoblin.processing import (
     26    FilenameBuilder, ProcessingManager)
     27
     28
     29_log = logging.getLogger(__name__)
     30
     31MEDIA_TYPE = 'mediagoblin.media_types.raw_image'
     32ACCEPTED_EXTENSIONS = ['nef',]
     33
     34
     35# The entire function have to be copied
     36
     37def sniff_handler(media_file, filename):
     38    _log.info('Sniffing {0}'.format(MEDIA_TYPE))
     39    name, ext = os.path.splitext(filename)
     40    clean_ext = ext[1:].lower()  # Strip the . from ext and make lowercase
     41
     42    if clean_ext in ACCEPTED_EXTENSIONS:
     43        _log.info('Found file extension in supported filetypes')
     44        return MEDIA_TYPE
     45    else:
     46        _log.debug('Media present, extension not found in {0}'.format(
     47                ACCEPTED_EXTENSIONS))
     48
     49    return None
     50
     51
     52class InitialRawProcessor(InitialProcessor):
     53    def common_setup(self):
     54        """
     55        Pull out a full-size JPEG-preview
     56        """
     57        super(self.__class__, self).common_setup()
     58
     59        self._original_raw = self.process_filename
     60
     61        # Read EXIF data
     62        md = pyexiv2.ImageMetadata(self._original_raw)
     63        md.read()
     64        self.process_filename = os.path.join(self.conversions_subdir,
     65            self.entry.queued_media_file[-1])
     66
     67        # Extract the biggest preview and write it as our working image
     68        md.previews[-1].write_to_file(
     69            self.process_filename.encode('utf-8'))
     70        self.process_filename += ".jpg"
     71        _log.debug('Wrote new file from {0} to preview (jpg) {1}'.format(
     72            self._original_raw, self.process_filename))
     73
     74        # Override the namebuilder with our new jpg-based name
     75        self.name_builder = FilenameBuilder(self.process_filename)
     76
     77
     78class RawImageProcessingManager(ProcessingManager):
     79    def __init__(self):
     80        super(self.__class__, self).__init__()
     81        self.add_processor(InitialRawProcessor)
     82        self.add_processor(Resizer)