Commit 3a88a7ee authored by nanahira's avatar nanahira

Update console-api/src/s3/s3.service.ts,...

Update console-api/src/s3/s3.service.ts, console-api/src/update/update.service.ts, console-api/src/entities/Archive.entity.ts, console-api/views/metalinks.mustache files
parent 9c117d90
Pipeline #39332 failed with stages
in 5 minutes and 46 seconds
...@@ -54,4 +54,11 @@ export class Archive extends TimeBase { ...@@ -54,4 +54,11 @@ export class Archive extends TimeBase {
name: this.archiveFullPath, name: this.archiveFullPath,
}; };
} }
url: string;
fillUrl(cdnUrl: string, cdnUrlOversize: string) {
const useCdn = this.size > 536870912 ? cdnUrlOversize : cdnUrl;
this.url = `${useCdn}/${this.path}.tar.zst`;
}
} }
...@@ -28,10 +28,19 @@ export class S3Service extends ConsoleLogger { ...@@ -28,10 +28,19 @@ export class S3Service extends ConsoleLogger {
return `${this.cdnUrl}/${path}`; return `${this.cdnUrl}/${path}`;
} }
getCdnUrlOversize(path: string) {
return `${this.cdnUrlOversize}/${path}`;
}
getCdnUrlAuto(path: string, size: number) {
return size > 536870912 ? this.getCdnUrlOversize(path) : this.getCdnUrl(path);
}
private readonly bucket = this.getConfig('S3_BUCKET'); private readonly bucket = this.getConfig('S3_BUCKET');
private readonly prefix = this.getConfig('S3_PREFIX'); private readonly prefix = this.getConfig('S3_PREFIX');
public readonly cdnUrl = public readonly cdnUrl =
this.getConfig('S3_CDN_URL') || `${this.getConfig('S3_ENDPOINT')}/${this.bucket}${this.prefix ? `/${this.prefix}` : ''}`; this.getConfig('S3_CDN_URL') || `${this.getConfig('S3_ENDPOINT')}/${this.bucket}${this.prefix ? `/${this.prefix}` : ''}`;
public readonly cdnUrlOversize = this.getConfig('S3_CDN_URL_OVERSIZE') || this.cdnUrl;
private readonly s3Config: S3ClientConfig = { private readonly s3Config: S3ClientConfig = {
credentials: { credentials: {
accessKeyId: this.getConfig('S3_KEY'), accessKeyId: this.getConfig('S3_KEY'),
......
...@@ -16,6 +16,7 @@ import moment from 'moment'; ...@@ -16,6 +16,7 @@ import moment from 'moment';
@Injectable() @Injectable()
export class UpdateService extends ConsoleLogger { export class UpdateService extends ConsoleLogger {
private readonly cdnUrl: string; private readonly cdnUrl: string;
private readonly cdnUrlOversize: string;
constructor( constructor(
@InjectConnection('app') @InjectConnection('app')
private db: Connection, private db: Connection,
...@@ -24,6 +25,7 @@ export class UpdateService extends ConsoleLogger { ...@@ -24,6 +25,7 @@ export class UpdateService extends ConsoleLogger {
) { ) {
super('update'); super('update');
this.cdnUrl = packageS3.cdnUrl; this.cdnUrl = packageS3.cdnUrl;
this.cdnUrlOversize = packageS3.cdnUrlOversize;
} }
async getAppsJson() { async getAppsJson() {
...@@ -150,8 +152,8 @@ export class UpdateService extends ConsoleLogger { ...@@ -150,8 +152,8 @@ export class UpdateService extends ConsoleLogger {
throw new BlankReturnMessageDto(404, 'Archive not found').toException(); throw new BlankReturnMessageDto(404, 'Archive not found').toException();
} }
await this.mirror.lookForArchivesMirror(archives, ip); await this.mirror.lookForArchivesMirror(archives, ip);
archives.forEach(a => a.fillUrl(this.cdnUrl, this.cdnUrlOversize));
return { return {
cdnUrl: this.cdnUrl,
archives, archives,
}; };
} }
...@@ -161,8 +163,8 @@ export class UpdateService extends ConsoleLogger { ...@@ -161,8 +163,8 @@ export class UpdateService extends ConsoleLogger {
qb.select(['archive.hash', 'archive.path', 'archive.size']).andWhere('archive.role = :fullRole', { fullRole: ArchiveType.Full }) qb.select(['archive.hash', 'archive.path', 'archive.size']).andWhere('archive.role = :fullRole', { fullRole: ArchiveType.Full })
); );
await this.mirror.lookForArchivesMirror(archives, ip); await this.mirror.lookForArchivesMirror(archives, ip);
archives.forEach(a => a.fillUrl(this.cdnUrl, this.cdnUrlOversize));
return { return {
cdnUrl: this.cdnUrl,
archives: archives, archives: archives,
}; };
} }
...@@ -251,8 +253,8 @@ export class UpdateService extends ConsoleLogger { ...@@ -251,8 +253,8 @@ export class UpdateService extends ConsoleLogger {
if (!remainingFiles.length) { if (!remainingFiles.length) {
// console.log('exact', bestUpdateArchive); // console.log('exact', bestUpdateArchive);
await this.mirror.lookForArchivesMirror([bestUpdateArchive], ip); await this.mirror.lookForArchivesMirror([bestUpdateArchive], ip);
bestUpdateArchive.fillUrl(this.cdnUrl, this.cdnUrlOversize);
return { return {
cdnUrl: this.cdnUrl,
archives: [bestUpdateArchive], archives: [bestUpdateArchive],
}; };
} }
...@@ -308,8 +310,8 @@ export class UpdateService extends ConsoleLogger { ...@@ -308,8 +310,8 @@ export class UpdateService extends ConsoleLogger {
const archives = _.minBy(packagePlans, (plan) => this.getCostOfArchives(plan)); const archives = _.minBy(packagePlans, (plan) => this.getCostOfArchives(plan));
// console.log('use', archives); // console.log('use', archives);
await this.mirror.lookForArchivesMirror(archives, ip); await this.mirror.lookForArchivesMirror(archives, ip);
archives.forEach(a => a.fillUrl(this.cdnUrl, this.cdnUrlOversize));
return { return {
cdnUrl: this.cdnUrl,
archives, archives,
}; };
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
{{#mirrors}} {{#mirrors}}
<url priority="1">{{&.}}</url> <url priority="1">{{&.}}</url>
{{/mirrors}} {{/mirrors}}
<url priority="2">{{&cdnUrl}}/{{path}}.tar.zst</url> <url priority="2">{{&url}}</url>
</file> </file>
{{/archives}} {{/archives}}
</metalink> </metalink>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment