Files
directus/api/src/storage.ts
Rijk van Zanten 35830a5dfe Move Flydrive home and implement Azure (#4110)
* Add new packages

* Update docs

* Update linked packages

* Setup file pointers

* Don't require getStream to be async

* List credits

* Load azure in storage

* Fix typo in docs

* Fix another typo

* Remove not about raising an issue
2021-02-16 18:33:50 -05:00

73 lines
1.9 KiB
TypeScript

import { StorageManager, LocalFileSystemStorage, StorageManagerConfig, Storage } from '@directus/drive';
import env from './env';
import { validateEnv } from './utils/validate-env';
import { getConfigFromEnv } from './utils/get-config-from-env';
import { toArray } from './utils/to-array';
/** @todo dynamically load these storage adapters */
import { AmazonWebServicesS3Storage } from '@directus/drive-s3';
import { GoogleCloudStorage } from '@directus/drive-gcs';
import { AzureBlobWebServicesStorage } from '@directus/drive-azure';
validateEnv(['STORAGE_LOCATIONS']);
const storage = new StorageManager(getStorageConfig());
registerDrivers(storage);
export default storage;
function getStorageConfig(): StorageManagerConfig {
const config: StorageManagerConfig = {
disks: {},
};
const locations = toArray(env.STORAGE_LOCATIONS);
locations.forEach((location: string) => {
location = location.trim();
const diskConfig = {
driver: env[`STORAGE_${location.toUpperCase()}_DRIVER`],
config: getConfigFromEnv(`STORAGE_${location.toUpperCase()}_`),
};
delete diskConfig.config.publicUrl;
delete diskConfig.config.driver;
config.disks![location] = diskConfig;
});
return config;
}
function registerDrivers(storage: StorageManager) {
const usedDrivers: string[] = [];
for (const [key, value] of Object.entries(env)) {
if ((key.startsWith('STORAGE') && key.endsWith('DRIVER')) === false) continue;
if (value && usedDrivers.includes(value) === false) usedDrivers.push(value);
}
usedDrivers.forEach((driver) => {
const storageDriver = getStorageDriver(driver);
if (storageDriver) {
storage.registerDriver<Storage>(driver, storageDriver);
}
});
}
function getStorageDriver(driver: string) {
switch (driver) {
case 'local':
return LocalFileSystemStorage;
case 's3':
return AmazonWebServicesS3Storage;
case 'gcs':
return GoogleCloudStorage;
case 'azure':
return AzureBlobWebServicesStorage;
}
}