This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+147
View File
@@ -0,0 +1,147 @@
/// <reference path="../../adonis-typings/index.d.ts" />
/// <reference path="../../adonis-typings/drive.d.ts" />
/// <reference path="../../test-helpers/drive.d.ts" />
/// <reference types="@adonisjs/logger/build/adonis-typings/logger" />
/// <reference types="node" />
/// <reference types="node" />
import { Manager } from '@poppinss/manager';
import { RouterContract } from '@ioc:Adonis/Core/Route';
import { LoggerContract } from '@ioc:Adonis/Core/Logger';
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
import { DisksList, Visibility, DriveConfig, DriverContract, DriveFileStats, LocalDriverConfig, DriveManagerContract, FakeImplementationCallback, DirectoryListingContract, DriveListItem } from '@ioc:Adonis/Core/Drive';
import { FakeDrive } from '../Fake';
/**
* Drive manager exposes the API to resolve disks and extend by
* adding custom drivers
*/
export declare class DriveManager extends Manager<ApplicationContract, DriverContract, DriverContract, {
[P in keyof DisksList]: DisksList[P]['implementation'];
}> implements DriveManagerContract {
application: ApplicationContract;
router: RouterContract;
private logger;
private config;
/**
* Find if drive is ready to be used
*/
private isReady;
/**
* The fake callback
*/
private fakeCallback;
/**
* Reference to the fake drive
*/
private fakeDrive;
/**
* Cache all disks instances
*/
protected singleton: boolean;
/**
* Reference to registered fakes
*/
fakes: Map<"local", import("@ioc:Adonis/Core/Drive").FakeDriverContract>;
constructor(application: ApplicationContract, router: RouterContract, logger: LoggerContract, config: DriveConfig);
/**
* Validate config
*/
private validateConfig;
/**
* Returns the default mapping name
*/
protected getDefaultMappingName(): "local";
/**
* Returns config for a given mapping
*/
protected getMappingConfig(diskName: keyof DisksList): LocalDriverConfig;
/**
* Returns the name of the drive used by a given mapping
*/
protected getMappingDriver(diskName: keyof DisksList): string | undefined;
/**
* Make instance of the local driver
*/
protected createLocal(diskName: keyof DisksList, config: LocalDriverConfig): any;
/**
* Fake default or a named disk
*/
fake(disks?: keyof DisksList | keyof DisksList[]): FakeDrive;
/**
* Restore the fake for the default or a named disk
*/
restore(disks?: keyof DisksList | keyof DisksList[]): void;
/**
* Restore all fakes1
*/
restoreAll(): void;
/**
* Resolve instance for a disk
*/
use(disk?: keyof DisksList): any;
/**
* Register a custom fake implementation
*/
setFakeImplementation(callback: FakeImplementationCallback): void;
/**
* Returns the file contents as a buffer. The buffer return
* value allows you to self choose the encoding when
* converting the buffer to a string.
*/
get(location: string, ...args: any[]): Promise<Buffer>;
/**
* Returns the file contents as a stream
*/
getStream(location: string, ...args: any[]): Promise<NodeJS.ReadableStream>;
/**
* A boolean to find if the location path exists or not
*/
exists(location: string, ...args: any[]): Promise<boolean>;
/**
* Returns the location path visibility
*/
getVisibility(location: string, ...args: any[]): Promise<Visibility>;
/**
* Returns the location path stats
*/
getStats(location: string, ...args: any[]): Promise<DriveFileStats>;
/**
* Returns a signed URL for a given location path
*/
getSignedUrl(location: string, ...args: any[]): Promise<string>;
/**
* Returns a URL for a given location path
*/
getUrl(location: string, ...args: any[]): Promise<string>;
/**
* Write string|buffer contents to a destination. The missing
* intermediate directories will be created (if required).
*/
put(location: string, ...args: any[]): Promise<void>;
/**
* Write a stream to a destination. The missing intermediate
* directories will be created (if required).
*/
putStream(location: string, ...args: any[]): Promise<void>;
/**
* Not supported
*/
setVisibility(location: string, ...args: any[]): Promise<void>;
/**
* Remove a given location path
*/
delete(location: string, ...args: any[]): Promise<void>;
/**
* Copy a given location path from the source to the desination.
* The missing intermediate directories will be created (if required)
*/
copy(source: string, ...args: any[]): Promise<void>;
/**
* Move a given location path from the source to the desination.
* The missing intermediate directories will be created (if required)
*/
move(source: string, ...args: any[]): Promise<void>;
/**
* Return a listing directory iterator for given location.
*/
list(location: string): DirectoryListingContract<DriverContract, DriveListItem>;
}
+236
View File
@@ -0,0 +1,236 @@
"use strict";
/*
* @adonisjs/drive
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DriveManager = void 0;
/// <reference path="../../adonis-typings/index.ts" />
const manager_1 = require("@poppinss/manager");
const utils_1 = require("@poppinss/utils");
const Fake_1 = require("../Fake");
/**
* Drive manager exposes the API to resolve disks and extend by
* adding custom drivers
*/
class DriveManager extends manager_1.Manager {
constructor(application, router, logger, config) {
super(application);
this.application = application;
this.router = router;
this.logger = logger;
this.config = config;
/**
* Find if drive is ready to be used
*/
this.isReady = false;
/**
* The fake callback
*/
this.fakeCallback = (_, disk, config) => {
const { FakeDriver } = require('../Drivers/Fake');
return new FakeDriver(disk, config, this.router);
};
/**
* Reference to the fake drive
*/
this.fakeDrive = new Fake_1.FakeDrive();
/**
* Cache all disks instances
*/
this.singleton = true;
/**
* Reference to registered fakes
*/
this.fakes = this.fakeDrive.fakes;
this.validateConfig();
}
/**
* Validate config
*/
validateConfig() {
if (!this.config) {
return;
}
const validator = new utils_1.ManagerConfigValidator(this.config, 'drive', 'config/drive');
validator.validateDefault('disk');
validator.validateList('disks', 'disk');
this.isReady = true;
}
/**
* Returns the default mapping name
*/
getDefaultMappingName() {
return this.config.disk;
}
/**
* Returns config for a given mapping
*/
getMappingConfig(diskName) {
return this.config.disks[diskName];
}
/**
* Returns the name of the drive used by a given mapping
*/
getMappingDriver(diskName) {
return this.getMappingConfig(diskName)?.driver;
}
/**
* Make instance of the local driver
*/
createLocal(diskName, config) {
const { LocalDriver } = require('../Drivers/Local');
return new LocalDriver(diskName, config, this.router);
}
/**
* Fake default or a named disk
*/
fake(disks) {
disks = disks || this.getDefaultMappingName();
const disksToFake = Array.isArray(disks) ? disks : [disks];
disksToFake.forEach((disk) => {
if (!this.fakeDrive.isFaked(disk)) {
this.logger.trace({ disk: disk }, 'drive faking disk');
this.fakeDrive.fakes.set(disk, this.fakeCallback(this, disk, this.getMappingConfig(disk)));
}
});
return this.fakeDrive;
}
/**
* Restore the fake for the default or a named disk
*/
restore(disks) {
disks = disks || this.getDefaultMappingName();
const disksToRestore = Array.isArray(disks) ? disks : [disks];
disksToRestore.forEach((disk) => {
if (this.fakeDrive.isFaked(disk)) {
this.logger.trace({ disk: disk }, 'drive restoring disk fake');
this.fakeDrive.restore(disk);
}
});
}
/**
* Restore all fakes1
*/
restoreAll() {
this.fakeDrive.fakes = new Map();
}
/**
* Resolve instance for a disk
*/
use(disk) {
if (!this.isReady) {
throw new utils_1.Exception('Missing configuration for drive. Visit https://bit.ly/2WnR5j9 for setup instructions', 500, 'E_MISSING_DRIVE_CONFIG');
}
disk = disk || this.getDefaultMappingName();
if (this.fakeDrive.isFaked(disk)) {
return this.fakeDrive.use(disk);
}
return super.use(disk);
}
/**
* Register a custom fake implementation
*/
setFakeImplementation(callback) {
this.fakeCallback = callback;
}
/**
* Returns the file contents as a buffer. The buffer return
* value allows you to self choose the encoding when
* converting the buffer to a string.
*/
async get(location, ...args) {
return this.use().get(location, ...args);
}
/**
* Returns the file contents as a stream
*/
async getStream(location, ...args) {
return this.use().getStream(location, ...args);
}
/**
* A boolean to find if the location path exists or not
*/
exists(location, ...args) {
return this.use().exists(location, ...args);
}
/**
* Returns the location path visibility
*/
async getVisibility(location, ...args) {
return this.use().getVisibility(location, ...args);
}
/**
* Returns the location path stats
*/
async getStats(location, ...args) {
return this.use().getStats(location, ...args);
}
/**
* Returns a signed URL for a given location path
*/
getSignedUrl(location, ...args) {
return this.use().getSignedUrl(location, ...args);
}
/**
* Returns a URL for a given location path
*/
getUrl(location, ...args) {
return this.use().getUrl(location, ...args);
}
/**
* Write string|buffer contents to a destination. The missing
* intermediate directories will be created (if required).
*/
put(location, ...args) {
return this.use().put(location, ...args);
}
/**
* Write a stream to a destination. The missing intermediate
* directories will be created (if required).
*/
putStream(location, ...args) {
return this.use().putStream(location, ...args);
}
/**
* Not supported
*/
setVisibility(location, ...args) {
return this.use().setVisibility(location, ...args);
}
/**
* Remove a given location path
*/
delete(location, ...args) {
return this.use().delete(location, ...args);
}
/**
* Copy a given location path from the source to the desination.
* The missing intermediate directories will be created (if required)
*/
copy(source, ...args) {
return this.use().copy(source, ...args);
}
/**
* Move a given location path from the source to the desination.
* The missing intermediate directories will be created (if required)
*/
move(source, ...args) {
return this.use().move(source, ...args);
}
/**
* Return a listing directory iterator for given location.
*/
list(location) {
const driver = this.use();
if (typeof driver.list !== 'function') {
throw new utils_1.Exception(`List is not supported by the "${driver.name}" driver.`, 500, 'E_LIST_NOT_SUPPORTED');
}
return driver.list(location);
}
}
exports.DriveManager = DriveManager;