Skip to content

Commit

Permalink
feat: add MultipartFile.moveToDisk method to the bodyparser
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 11, 2024
1 parent b41864b commit 5659418
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions providers/drive_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

import { Disk, DriveManager } from 'flydrive'
import { configProvider } from '@adonisjs/core'
import { MultipartFile } from '@adonisjs/core/bodyparser'
import { RuntimeException } from '@adonisjs/core/exceptions'
import type { ApplicationService } from '@adonisjs/core/types'

import { createFileServer } from '../src/file_server.js'
import type { DriveService, ServiceWithLocalServer, SignedURLOptions } from '../src/types.js'
import debug from '../src/debug.js'
import { createFileServer } from '../src/file_server.js'
import type {
DriveDisks,
DriveService,
WriteOptions,
SignedURLOptions,
ServiceWithLocalServer,
} from '../src/types.js'

/**
* Extending the container with a custom service
Expand All @@ -25,6 +32,22 @@ declare module '@adonisjs/core/types' {
}
}

/**
* Extending BodyParser Multipart file with "moveToDisk"
* method to move file from the local filesystem to
* a drive disk
*/
declare module '@adonisjs/core/bodyparser' {
interface MultipartFile {
/**
* Move user uploaded file from the tmp directory
* to a Drive disk
*/
moveToDisk(key: string, disk?: keyof DriveDisks, options?: WriteOptions): Promise<void>
moveToDisk(key: string, options?: WriteOptions): Promise<void>
}
}

/**
* Drive Provider registers a singleton drive manager services
* to the IoC container and wires up the routing to serve
Expand Down Expand Up @@ -76,6 +99,44 @@ export default class DriveProvider {
}
}

/**
* Extending BodyParser Multipart file with "moveToDisk"
* method to move file from the local filesystem to
* a drive disk
*/
protected async extendMultipartFile(drive: DriveManager<any>) {
debug('Adding "MultipartFile.moveToDisk" method')

MultipartFile.macro(
'moveToDisk',
async function (this: MultipartFile, key, diskNameOrOptions?, writeOptions?) {
if (!this.tmpPath) {
throw new RuntimeException(
'property "tmpPath" must be set on the file before moving it',
{
status: 500,
code: 'E_MISSING_FILE_TMP_PATH',
}
)
}

let diskName: string | undefined
let options: WriteOptions | undefined

if (typeof diskNameOrOptions === 'string') {
diskName = diskNameOrOptions
} else if (diskNameOrOptions && !writeOptions) {
options = diskNameOrOptions
} else if (writeOptions) {
options = writeOptions
}

const disk = diskName ? drive.use(diskName) : drive.use()
return disk.moveFromFs(this.tmpPath, key, options)
}
)
}

register() {
this.app.container.singleton('drive.manager', async () => {
/**
Expand Down

0 comments on commit 5659418

Please sign in to comment.