Replace path with universal paths

This commit is contained in:
2020-09-05 11:35:15 -05:00
parent b780d35db1
commit e2efc6f39f
7 changed files with 37 additions and 60 deletions

View File

@@ -4,6 +4,7 @@ import {file_server} from '../../external/std.ts'
import {HTTPStatus} from '../../const/http.ts'
import {Logging} from '../../service/logging/Logging.ts'
import {Injectable} from '../../../../di/src/decorator/Injection.ts'
import {PathLike, universal_path} from '../../support/UniversalPath.ts'
/**
* Response factory to send the contents of a file given its path.
@@ -18,7 +19,7 @@ export default class FileResponseFactory extends ResponseFactory {
* The path to the file to be sent.
* @type string
*/
public readonly path: string,
public readonly path: PathLike,
) { super() }
/**
@@ -29,14 +30,16 @@ export default class FileResponseFactory extends ResponseFactory {
public async write(request: Request): Promise<Request> {
request = await super.write(request)
const content = await file_server.serveFile(request.to_native, this.path)
const path = typeof this.path === 'string' ? universal_path(this.path) : this.path
const content = await file_server.serveFile(request.to_native, path.to_local)
const length = content.headers && content.headers.get('content-length')
if ( content.headers && content.body && length ) {
request.response.body = content.body
request.response.headers.set('Content-Length', length)
} else {
this.logger.debug(`Tried to serve file that does not exist: ${this.path}`)
this.logger.debug(`Tried to serve file that does not exist: ${path}`)
request.response.status = HTTPStatus.NOT_FOUND
}

View File

@@ -10,6 +10,7 @@ import HTTPErrorResponseFactory from './HTTPErrorResponseFactory.ts'
import HTTPError from '../../error/HTTPError.ts'
import ViewResponseFactory from './ViewResponseFactory.ts'
import FileResponseFactory from './FileResponseFactory.ts'
import {PathLike} from '../../support/UniversalPath.ts'
/**
* Get a new JSON response factory that writes the given object as JSON.
@@ -83,6 +84,6 @@ export function view(view: string, data?: any): ViewResponseFactory {
* @param {string} path
* @return FileResponseFactory
*/
export function file(path: string): FileResponseFactory {
export function file(path: PathLike): FileResponseFactory {
return make(FileResponseFactory, path)
}

View File

@@ -4,6 +4,7 @@ import {Injectable} from '../../../../di/src/decorator/Injection.ts'
import {file, http} from '../response/helpers.ts'
import {HTTPStatus} from '../../const/http.ts'
import {Logging} from '../../service/logging/Logging.ts'
import {UniversalPath} from "../../support/UniversalPath.ts";
/**
* Daton-provided middleware that serves files from a static directory.
@@ -51,7 +52,7 @@ export default class StaticServer extends Middleware {
* @param {string} rel_file
* @return string
*/
protected resolve(asset_dir: string, rel_file: string): string {
protected resolve(asset_dir: string, rel_file: string): UniversalPath {
return this.app.path(asset_dir, rel_file)
}
@@ -60,9 +61,9 @@ export default class StaticServer extends Middleware {
* @param {string} path
* @return Promise<boolean>
*/
protected async fileExists(path: string): Promise<boolean> {
protected async fileExists(path: UniversalPath): Promise<boolean> {
try {
const stat = await Deno.lstat(path)
const stat = await Deno.lstat(path.to_local)
return stat && stat.isFile
} catch (e) {
if ( e && e instanceof Deno.errors.NotFound ) {