mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 12:36:57 +00:00
upd: add logger, check hostname, add catch
This commit is contained in:
parent
381ea14049
commit
621f2e2ee4
2 changed files with 23 additions and 3 deletions
|
@ -12,12 +12,16 @@ import type { MiMeta } from '@/models/Meta.js';
|
|||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
||||
import Logger from '@/logger.js';
|
||||
|
||||
@Injectable()
|
||||
export class BunnyService {
|
||||
private bunnyCdnLogger: Logger;
|
||||
|
||||
constructor(
|
||||
private httpRequestService: HttpRequestService,
|
||||
) {
|
||||
this.bunnyCdnLogger = new Logger('bunnycdn', 'blue');
|
||||
}
|
||||
|
||||
@bindThis
|
||||
|
@ -40,7 +44,8 @@ export class BunnyService {
|
|||
|
||||
@bindThis
|
||||
public usingBunnyCDN(meta: MiMeta) {
|
||||
return meta.objectStorageEndpoint && meta.objectStorageEndpoint.includes('bunnycdn.com') ? true : false;
|
||||
const client = this.getBunnyInfo(meta);
|
||||
return new URL(client.fullUrl).hostname.endsWith('bunnycdn.com');
|
||||
}
|
||||
|
||||
@bindThis
|
||||
|
@ -66,8 +71,19 @@ export class BunnyService {
|
|||
|
||||
const req = https.request(options);
|
||||
|
||||
// Log and throw error if BunnyCDN detects wrong data and return to prevent console spam as this event occurs multiple times
|
||||
req.on('response', (res) => {
|
||||
if (res.statusCode === 401) {
|
||||
this.bunnyCdnLogger.error('Invalid AccessKey or region hostname');
|
||||
data.destroy();
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
console.error(error);
|
||||
this.bunnyCdnLogger.error(error);
|
||||
data.destroy();
|
||||
throw new IdentifiableError('689ee33f-f97c-479a-ac49-1b9f8140bf91', 'An error has occured during the connectiong to BunnyCDN');
|
||||
});
|
||||
|
||||
data.pipe(req).on('finish', () => {
|
||||
|
|
|
@ -408,7 +408,11 @@ export class DriveService {
|
|||
if (this.meta.objectStorageSetPublicRead) params.ACL = 'public-read';
|
||||
|
||||
if (this.bunnyService.usingBunnyCDN(this.meta)) {
|
||||
await this.bunnyService.upload(this.meta, key, stream);
|
||||
await this.bunnyService.upload(this.meta, key, stream).catch(
|
||||
err => {
|
||||
this.registerLogger.error(`Upload Failed: key = ${key}, filename = ${filename}`, err);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
await this.s3Service.upload(this.meta, params)
|
||||
.then(
|
||||
|
|
Loading…
Add table
Reference in a new issue