set RESOURCE_TYPE based file extension
Hello,
I'm trying to figure out how to set the RESOURCE_TYPE based file extension in Django, but I'm not sure if my solution is correct.
I'm basically overriding the _get_resource_type method, which seems to work well for uploading images and videos based on file extension, but when I try to access my files, the images show, but the videos don't. The url seems to still say /image/ instead of /video/
Any ideas on how I can fix that?
Thank you very much!
-
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 -
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 -
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 extentionsHope this helps.
1 -
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 -
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
Post is closed for comments.
Comments
5 comments