// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using FluentFTP; using MongoDB.Driver.GridFS; using Squidex.Assets; using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Domain.Apps.Entities.Assets.Queries; using Squidex.Domain.Apps.Entities.History; using Squidex.Domain.Apps.Entities.Search; using Squidex.Hosting; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Orleans; using tusdotnet.Interfaces; namespace Squidex.Config.Domain { public static class AssetServices { public static void AddSquidexAssets(this IServiceCollection services, IConfiguration config) { services.Configure(config, "assets"); if (config.GetValue("assets:deleteRecursive")) { services.AddTransientAs() .As(); } if (config.GetValue("assets:deletePermanent")) { services.AddTransientAs() .As(); } services.AddSingletonAs() .AsSelf(); services.AddTransientAs() .As(); services.AddTransientAs() .As(); services.AddSingletonAs() .AsSelf(); services.AddSingletonAs() .As().As(); services.AddSingletonAs() .AsSelf(); services.AddTransientAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As().As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As().As().As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs() .As(); services.AddSingletonAs>() .AsSelf(); } public static void AddSquidexAssetInfrastructure(this IServiceCollection services, IConfiguration config) { config.ConfigureByOption("assetStore:type", new Alternatives { ["Default"] = () => { services.AddSingletonAs() .AsOptional(); }, ["Folder"] = () => { var path = config.GetRequiredValue("assetStore:folder:path"); services.AddSingletonAs(c => new FolderAssetStore(path, c.GetRequiredService>())) .As(); }, ["GoogleCloud"] = () => { var options = new GoogleCloudAssetOptions { BucketName = config.GetRequiredValue("assetStore:googleCloud:bucket") }; services.AddSingletonAs(c => new GoogleCloudAssetStore(options)) .As(); }, ["AzureBlob"] = () => { var options = new AzureBlobAssetOptions { ConnectionString = config.GetRequiredValue("assetStore:azureBlob:connectionString"), ContainerName = config.GetRequiredValue("assetStore:azureBlob:containerName") }; services.AddSingletonAs(c => new AzureBlobAssetStore(options)) .As(); }, ["AmazonS3"] = () => { var amazonS3Options = config.GetSection("assetStore:amazonS3").Get() ?? new (); services.AddSingletonAs(c => new AmazonS3AssetStore(amazonS3Options)) .As(); }, ["MongoDb"] = () => { var mongoConfiguration = config.GetRequiredValue("assetStore:mongoDb:configuration"); var mongoDatabaseName = config.GetRequiredValue("assetStore:mongoDb:database"); var mongoGridFsBucketName = config.GetRequiredValue("assetStore:mongoDb:bucket"); services.AddSingletonAs(c => { var mongoClient = StoreServices.GetMongoClient(mongoConfiguration); var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName); var gridFsbucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions { BucketName = mongoGridFsBucketName }); return new MongoGridFsAssetStore(gridFsbucket); }) .As(); }, ["Ftp"] = () => { var serverHost = config.GetRequiredValue("assetStore:ftp:serverHost"); var serverPort = config.GetOptionalValue("assetStore:ftp:serverPort", 21); var username = config.GetRequiredValue("assetStore:ftp:username"); var password = config.GetRequiredValue("assetStore:ftp:password"); var options = new FTPAssetOptions { Path = config.GetOptionalValue("assetStore:ftp:path", "/") }; services.AddSingletonAs(c => { var factory = new Func(() => new FtpClient(serverHost, serverPort, username, password)); return new FTPAssetStore(factory, options, c.GetRequiredService>()); }) .As(); } }); services.AddSingletonAs(c => { var service = c.GetRequiredService(); return new DelegateInitializer(service.GetType().Name, service.InitializeAsync); }); } } }