set RESOURCE_TYPE based file extension

Comments

5 comments

  • Avatar
    Loic Verger Del Bove

    Hi Karim,

    Could you share the code you are using to deliver assets? Normally, adding `resource_type="video"` should be enough.

    Thanks in advance.

    Best,

    Loic

    1
    Comment actions Permalink
  • Avatar
    Karim

    Hi Loic,

    Thank you for your response. You can find the code below.

    Also to give you a short explanation of what I'm trying to achieve, I'm basically trying to use the same Django FileField to upload images and videos.

    class VideoCloudinaryStorage(VideoMediaCloudinaryStorage):

    def _get_resource_type(self, name):
    extension = name.split('.')[-1].lower()
    if extension in app_settings.STATIC_IMAGES_EXTENSIONS:
    self.RESOURCE_TYPE = RESOURCE_TYPES['IMAGE']

    return self.RESOURCE_TYPE

    def _upload(self, name, content, video_settings=None):
    options = {'use_filename': True,
    'resource_type': self._get_resource_type(name),
    'tags': self.TAG}
    if video_settings:
    options.update(**video_settings)
    folder = os.path.dirname(name)
    if folder:
    options['folder'] = folder
    return cloudinary.uploader.upload(content, **options)
    1
    Comment actions Permalink
  • Avatar
    Eric Pasos

    Hi Karim,

    There are different resource types (see https://cloudinary.com/documentation/image_upload_api_reference#:~:text=parameter%20to%20attachment).-,resource_type,-String) that can be used depending on the asset that you are trying to upload. With your implementation, it is handling resource_type = 'image' only, and you will need to add the video, raw, and auto as well. For example:

    if extension in app_settings.STATIC_IMAGES_EXTENSIONS:
    self.RESOURCE_TYPE = RESOURCE_TYPES['IMAGE']

    #elif: check video extensions
    #else: check all other extentions

    Hope this helps.

    1
    Comment actions Permalink
  • Avatar
    Karim

    Hi Eric,

    Thank you very much for your response. After further checking and debugging, it turned out that the issue is mainly because file extensions are removed from the file name after it is uploaded.

    So when I try to open a file, the file "name" which gets passed to the _get_resource_type method does not contain the extension. Here is what it looks like: "media/dir/env/65/IMG_2508_opzzm3".

    How can I change the source type if there is no extension returned with the file name?

    0
    Comment actions Permalink
  • Avatar
    Eric Pasos

    Hi Karim,

    You can detect the type of file by using packages such as the Python-Magic (although, there are many other approaches as shared here). There are also python-magic examples as shown in this link as well. Using the information you can extract from the module, you can then identify the resource_type that is corresponding to the asset you are trying to upload to your Media Library account.  Alternatively, the resource_type=auto can also help to automatically identify the resource type during upload (see this link).

    Hope this helps.

     

    0
    Comment actions Permalink

Please sign in to leave a comment.