Package: baseapp-cloudflare-stream-field
Django Cloudflare Stream FieldThis app provides integration with Cloudflare Stream, where you can uploaddirectly to CloudflareusingTUS protocol.Install the packageAdd torequirements/base.txt:baseapp-cloudflare-stream-field@git+https://github.com/silverlogic/baseapp-backend.git@v0.1#subdirectory=baseapp-cloudflare-stream-fieldAdd the following to yoursettings/base.py:# CloudflareCLOUDFLARE_ACCOUNT_ID=env("CLOUDFLARE_ACCOUNT_ID")CLOUDFLARE_API_TOKEN=env("CLOUDFLARE_API_TOKEN")CLOUDFLARE_AUTH_EMAIL=env("CLOUDFLARE_AUTH_EMAIL")# Make sure to add the task routing for refresh_from_cloudflare and generate_download_urlCELERY_TASK_ROUTES={"baseapp_cloudflare_stream_field.tasks.refresh_from_cloudflare":{"exchange":"default","routing_key":"default",},"baseapp_cloudflare_stream_field.tasks.generate_download_url":{"exchange":"default","routing_key":"default",},}This package offers the option to post-process the original video by trimming it to a predefined duration. This process creates a new trimmed video and then deletes the original one. To enable this behavior, you should add the following configurations:CLOUDFLARE_VIDEO_MAX_DURATION_SECONDS=IntegernumberCLOUDFLARE_VIDEO_AUTOMATIC_TRIM=True/FalseIf you setCLOUDFLARE_VIDEO_AUTOMATIC_TRIM = True, you must also specify a value forCLOUDFLARE_VIDEO_MAX_DURATION_SECONDS. This will trim the video to create a clip starting from the beginning and lasting up to your defined maximum duration. For example:CLOUDFLARE_VIDEO_MAX_DURATION_SECONDS=30CLOUDFLARE_VIDEO_AUTOMATIC_TRIM=True# Will create a new video from 0s to 30sIfCLOUDFLARE_VIDEO_AUTOMATIC_TRIMis not set to True, the package will create a request to Cloudflare with the maxDurationSeconds parameter. This means that it will attempt to fail uploads where the video's duration exceeds the set value.However, this feature is not fully implemented yet, so its use is not recommended at this time. Contributions to enhance this feature in our package are welcome.Include the URLs in your mainurls.pyfile:re_path(r"^cloudflare-stream-upload/",include("baseapp_cloudflare_stream_field.urls")),And if you use Django REST Framework, add the following to your router:# Cloudflare Stream Uploadfrombaseapp_cloudflare_stream_field.rest_frameworkimportCloudflareStreamUploadViewSetrouter.register(r"cloudflare-stream-upload",CloudflareStreamUploadViewSet,basename="cloudflare-stream-upload")Allow CORS HeadersYou need to addtus-resumable, upload-length, upload-metadata, upload-creatorto your CORS header. Add the following tosettings/base.py:CORS_ALLOW_HEADERS=["accept","accept-encoding","authorization","content-type","dnt","origin","user-agent","x-csrftoken","x-requested-with","tus-resumable","upload-length","upload-metadata","upload-creator"]UsageImport and use the field in your models file:frombaseapp_cloudflare_stream_fieldimportCloudflareStreamFieldclassPost(models.Model):video=CloudflareStreamField(null=True,blank=True,downloadable=False)If you setdownloadabletoTrueit will automatically trigger a task to generate and save the download url atobj.video['meta']['download_url'].CloudflareStreamFieldinherits fromJSONFieldso you can use any look it provides, like filter only for videos fully processed by Cloudflare:Post.objects.filter(video__status__state="ready")This package manages the video upload flow as follows:The client starts by creating a TUS (resumable upload protocol) request to your backend.Your server functions as a middleware, receiving this request and forwarding it to Cloudflare's endpoint.Upon receipt, Cloudflare provides a unique, one-time upload URL. TUS then manages the upload to this URL.Once the upload is complete, the client gets a response with the 'Stream-Media-ID' header, which contains a unique identifier for the video in Cloudflare.You then need to send this identifier (uid) back to your backend to create a new instance with the CloudflareStreamField. For example:Example:Model.objects.create(video=uid)This step ensures that your server's records are updated and in sync with the video's status on Cloudflare.