Beispiel #1
0
    def upload_files(self, uploads_bucket, directory):
        for file in os.listdir(directory):
            if file[0] == ".":
                # Ignore all files / directories that start with a period
                continue
            file_path = os.path.join(directory, file)
            if os.path.isdir(file_path):
                continue
            try:
                file_type = File.get_file_type(file)
            except UnknownFileType:
                log.warning(f"Unknown filetype for file: {file_path}")
                continue

            file_pk = file_path.replace(f"{IMPORT_DIRECTORY}/files/", "")

            log.info(f'Uploading {file_path} to bucket with key: "{file_pk}"')
            storage.upload_file(uploads_bucket, file, file_path)

            # Kind of hacky, but no other option on Linux
            file_date = datetime.fromtimestamp(os.path.getmtime(file_path))
            # Only create the file if does not exist
            File.objects.get_or_create(
                pk=file_pk,
                defaults={
                    "created_at": file_date,
                    "state": "ready",
                    "file_type": file_type,
                    "token": get_random_string(32),
                    "user": None,
                    "public": False,
                },
            )
Beispiel #2
0
    def test_create_file(self, mock_get_random_string,
                         mock_get_available_name):
        file = File.create_file("abakus.png", User.objects.get(id=1), True)

        self.assertEqual(file.key, mock_get_available_name.return_value)
        self.assertEqual(file.token, mock_get_random_string.return_value)
        self.assertEqual(file.file_type, "image")
        self.assertEqual(file.user_id, 1)
Beispiel #3
0
 def test_get_file_type(self):
     self.assertEqual(File.get_file_type("abakus.png"), "image")
     self.assertEqual(File.get_file_type("abakus.jpg"), "image")
     self.assertEqual(File.get_file_type("abakus.pdf"), "document")
     self.assertRaises(UnknownFileType, File.get_file_type, "abakus.mp3")
Beispiel #4
0
 def test_get_file_type(self):
     self.assertEqual(File.get_file_type('abakus.png'), 'image')
     self.assertEqual(File.get_file_type('abakus.jpg'), 'image')
     self.assertEqual(File.get_file_type('abakus.pdf'), 'document')
     self.assertRaises(UnknownFileType, File.get_file_type, 'abakus.mp3')