Example #1
0
    def upload_file(self,
                    path: str,
                    key: str,
                    cache_time: int = 600,
                    content_type: Optional[str] = None) -> None:
        """
        Upload file to S3 storage. Similar to the s3.upload_file, however, that
        does not work nicely with moto, whereas this function does.
        :param string path: file to upload
        :param string key: name of the object in the bucket
        """
        from general_tools.file_utils import get_mime_type
        #from app_settings.app_settings import AppSettings
        #AppSettings.logger.debug(f"s3_handler.upload_file({path}, {key}, {cache_time}, {content_type})")
        assert 'http' not in key.lower()

        with open(path, 'rb') as f:
            binary = f.read()
        if content_type is None:
            mime_type = get_mime_type(path)
            content_type = mime_type  # Let browser figure out the encoding
            # content_type = f'{mime_type}; charset=utf-8' if 'usfm' in mime_type \
            #                 else mime_type # RJH added charset Oct2019
        # from app_settings.app_settings import AppSettings
        # AppSettings.logger.debug(f"Uploading {path} to S3 {key} with cache_time={cache_time} content_type='{content_type}'…")
        # AppSettings.logger.debug(f"Bucket is {self.bucket}")
        self.bucket.put_object(Key=key,
                               Body=binary,
                               ContentType=content_type,
                               CacheControl=f'max-age={cache_time}')
Example #2
0
 def upload_file(self, path, key, cache_time=600):
     self.bucket.upload_file(path,
                             key,
                             ExtraArgs={
                                 'ContentType':
                                 get_mime_type(path),
                                 'CacheControl':
                                 'max-age={0}'.format(cache_time)
                             })
Example #3
0
 def process_file(self, path):
     stats = os.stat(path)
     file_path = '{0}/{1}/v{2}/{3}'.format(self.manifest.language['slug'],
                                           self.manifest.slug.split('-')[2],
                                           self.manifest.status['version'],
                                           os.path.basename(path))
     url = '{0}/{1}'.format(self.cdn_url, file_path)
     format = {
         'size': stats.st_size,
         'modified_at': stats.st_mtime,
         'mime_type': get_mime_type(path),
         'url': url,
         'sig': url + '.sig'
     }
     self.manifest.formats.append(format)
     self.files.append(file_path)
     self.s3_handler.upload_file(path, "temp/" + file_path)
Example #4
0
 def test_get_mime_type(self):
     self.tmp_dir = tempfile.mkdtemp(prefix='Door43_test_file_utils_')
     tmp_file = os.path.join(self.tmp_dir, 'hello.txt')
     with open(tmp_file, "w") as f:
         f.write("hello world")
     self.assertEqual(file_utils.get_mime_type(tmp_file), "text/plain")
Example #5
0
 def test_get_mime_type(self):
     tmp_dir = tempfile.mkdtemp()
     tmp_file = tmp_dir + "/hello.txt"
     with open(tmp_file, "w") as f:
         f.write("hello world")
     self.assertEqual(file_utils.get_mime_type(tmp_file), "text/plain")