2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as Path from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-12-04 09:05:32 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
|
|
|
|
const path = Path.resolve(_dirname, '../../../../files');
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InternalStorageService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public resolvePath(key: string) {
|
|
|
|
return Path.resolve(path, key);
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public read(key: string) {
|
|
|
|
return fs.createReadStream(this.resolvePath(key));
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public saveFromPath(key: string, srcPath: string) {
|
|
|
|
fs.mkdirSync(path, { recursive: true });
|
|
|
|
fs.copyFileSync(srcPath, this.resolvePath(key));
|
|
|
|
return `${this.config.url}/files/${key}`;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public saveFromBuffer(key: string, data: Buffer) {
|
|
|
|
fs.mkdirSync(path, { recursive: true });
|
|
|
|
fs.writeFileSync(this.resolvePath(key), data);
|
|
|
|
return `${this.config.url}/files/${key}`;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public del(key: string) {
|
|
|
|
fs.unlink(this.resolvePath(key), () => {});
|
|
|
|
}
|
|
|
|
}
|