@@ -0,0 +1,2212 @@
+#!/usr/bin/env python%0A%22%22%22%0ASet the Google Cloud Storage metadata for each content file that already exists.%0A%0ASets the following attributes:%0A- Content-Type: sets it to a mimetype depending on its file extension.%0A- Cache-Control: We set this according to the following rules:%0A    - if the file ends in .sqlite3, then we tell GCS to cache this for 60 seconds, as this might be updated frequently by the user.%0A    - if it ends in any other file extension, then we cache the file for a month.%0A%22%22%22%0Aimport concurrent.futures%0Aimport pathlib%0Aimport os%0Aimport sys%0Afrom multiprocessing.pool import Pool%0A%0Aimport progressbar%0Afrom django.core.management.base import BaseCommand%0Afrom django.core.files.storage import default_storage%0A%0Afrom contentcuration.utils.gcs_storage import GoogleCloudStorage as gcs%0A%0Aclass Command(BaseCommand):%0A%0A    def handle(self, *args, **kwargs):%0A        blobs = self._list_all_files()%0A%0A        futures = %5B%5D%0A        with concurrent.futures.ThreadPoolExecutor() as e:%0A            print(%22Scheduling all metadata update jobs...%22)%0A            progbar = progressbar.ProgressBar()%0A            for blob in progbar(blobs):%0A                future = e.submit(self._update_metadata, blob)%0A                futures.append(future)%0A%0A            print(%22Waiting for all jobs to finish...%22)%0A            progbar = progressbar.ProgressBar(max_value=len(futures))%0A            for _ in progbar(concurrent.futures.as_completed(futures)):%0A                pass%0A%0A    def _determine_cache_control(self, name):%0A        _, ext = os.path.splitext(name)%0A%0A        if %22sqlite3%22 in ext:%0A            cache_control = %22public, max-age=60%22%0A        else:%0A            age = 3600 * 24 * 31  # one month%0A            cache_control = %22public, max-age=%7B%7D%22.format(age)%0A%0A        return cache_control%0A%0A    def _list_all_files(self):%0A        return default_storage.bucket.list_blobs()%0A%0A    def _update_metadata(self, blob):%0A        name = str(blob.name)%0A%0A        content_type = gcs._determine_content_type(name)%0A        cache_control = self._determine_cache_control(name)%0A        blob_update = %7B%0A            %22Content-Type%22: content_type,%0A            %22Cache-Control%22: cache_control,%0A        %7D%0A        blob.metadata = blob_update%0A        blob.patch()%0A

Add initial version of set_content_mimetypes mgmt command (#1009)