-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e932ae7
commit ee448ce
Showing
9 changed files
with
737 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type {Request, Response} from 'express' | ||
import got from 'got' | ||
import {join} from 'path' | ||
import {WebdavStorage} from './webdav.storage.js' | ||
|
||
export class AlistWebdavStorage extends WebdavStorage { | ||
public async express(hashPath: string, req: Request, res: Response): Promise<{ bytes: number; hits: number }> { | ||
const path = join(this.basePath, hashPath) | ||
const url = this.client.getFileDownloadLink(path) | ||
const resp = await got.get(url, {followRedirect: false, | ||
responseType: 'buffer', | ||
https: { | ||
rejectUnauthorized: false, | ||
}}) | ||
if (resp.statusCode === 200) { | ||
res.send(resp.body) | ||
return {bytes: resp.body.length, hits: 1} | ||
} | ||
if (resp.statusCode === 302 && resp.headers.location) { | ||
res.status(302).location(resp.headers.location).send() | ||
return {bytes: 0, hits: 1} | ||
} | ||
res.status(resp.statusCode).send(resp.body) | ||
return {bytes: 0, hits: 0} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
import type {NextFunction, Request, Response} from 'express' | ||
import {join} from 'path' | ||
import {cwd} from 'process' | ||
import type {Config} from '../config.js' | ||
import {logger} from '../logger.js' | ||
import {AlistWebdavStorage} from './alist-webdav.storage.js' | ||
import {FileStorage} from './file.storage.js' | ||
import {WebdavStorage} from './webdav.storage.js' | ||
|
||
export interface IStorage { | ||
init?(): Promise<void> | ||
writeFile(path: string, content: Buffer): Promise<void> | ||
|
||
exists(path: string): Promise<boolean> | ||
|
||
getAbsolutePath(path: string): string | ||
|
||
getMissingFiles<T extends {path: string; hash: string}>(files: T[]): Promise<T[]> | ||
|
||
gc(files: {path: string; hash: string; size: number}[]): Promise<void> | ||
|
||
express(hashPath: string, req: Request, res: Response, next?: NextFunction): Promise<{ bytes: number; hits: number }> | ||
} | ||
|
||
export function getStorage(config: Config): IStorage { | ||
return new FileStorage(join(cwd(), 'cache')) | ||
let storage: IStorage | ||
switch (config.storage) { | ||
case 'file': | ||
storage = new FileStorage(join(cwd(), 'cache')) | ||
break | ||
case 'webdav': | ||
storage = new WebdavStorage(config.storageOpts) | ||
break | ||
case 'alist': | ||
storage = new AlistWebdavStorage(config.storageOpts) | ||
break | ||
default: | ||
throw new Error(`未知的存储类型${config.storage}`) | ||
} | ||
logger.info(`使用存储类型: ${config.storage}`) | ||
return storage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.