Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions server/src/controllers/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,48 @@ export default (app: App) => {
tags: ["v2"],
},
)
.get(
"v2/filename/:filename",
async ({
BeatmapsManagerInstance,
params: { filename },
query: { full },
set,
}) => {
const beatmap = await BeatmapsManagerInstance.getBeatmap({
beatmapFilename: filename,
allowMissingNonBeatmapValues: full,
});

if (beatmap.source) {
set.headers["X-Data-Source"] = beatmap.source;
}

const { source: _, ...responseBeatmap } = beatmap;
if (!full || !beatmap.data)
return responseBeatmap?.data ?? responseBeatmap;

const beatmapset = await BeatmapsManagerInstance.getBeatmapSet({
beatmapSetId: beatmap.data.beatmapset_id,
});

if (beatmapset.source) {
set.headers["X-Data-Source"] = beatmapset.source;
}

const { source: __, ...responseBeatmapset } = beatmapset;
return responseBeatmapset?.data ?? responseBeatmapset;
},
{
params: t.Object({
filename: t.String(),
}),
query: t.Object({
full: t.Optional(t.BooleanString()),
}),
tags: ["v2"],
},
)
.get(
"v2/md5/:hash",
async ({
Expand Down
2 changes: 2 additions & 0 deletions server/src/core/abstracts/client/base-client.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type DownloadOsuBeatmap = {
export type GetBeatmapOptions = {
beatmapId?: number;
beatmapHash?: string;
beatmapFilename?: string;
allowMissingNonBeatmapValues?: boolean;
};

Expand All @@ -61,6 +62,7 @@ export enum ClientAbilities {
GetBeatmapsetsByBeatmapIds = 1 << 11, // 2048
GetBeatmapByIdWithSomeNonBeatmapValues = 1 << 12, // 4096
GetBeatmapByHashWithSomeNonBeatmapValues = 1 << 13, // 8192
GetBeatmapByFilename = 1 << 14, // 16384
}

export type MirrorClient<T extends BaseClient = BaseClient> = {
Expand Down
29 changes: 29 additions & 0 deletions server/src/core/domains/osu.ppy.sh/bancho.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class BanchoClient extends BaseClient {
abilities: [
ClientAbilities.GetBeatmapById,
ClientAbilities.GetBeatmapByHash,
ClientAbilities.GetBeatmapByFilename,
ClientAbilities.GetBeatmapSetById,
ClientAbilities.GetBeatmaps,
ClientAbilities.DownloadOsuBeatmap,
Expand All @@ -49,6 +50,7 @@ export class BanchoClient extends BaseClient {
abilities: [
ClientAbilities.GetBeatmapById,
ClientAbilities.GetBeatmapByHash,
ClientAbilities.GetBeatmapByFilename,
ClientAbilities.GetBeatmapSetById,
ClientAbilities.GetBeatmaps,
ClientAbilities.DownloadOsuBeatmap,
Expand Down Expand Up @@ -85,6 +87,9 @@ export class BanchoClient extends BaseClient {
else if (ctx.beatmapHash) {
return await this.getBeatmapByHash(ctx.beatmapHash);
}
else if (ctx.beatmapFilename) {
return await this.getBeatmapByFilename(ctx.beatmapFilename);
}

throw new Error("Invalid arguments");
}
Expand Down Expand Up @@ -293,6 +298,30 @@ export class BanchoClient extends BaseClient {
};
}

private async getBeatmapByFilename(
beatmapHash: string,
): Promise<ResultWithStatus<Beatmap>> {
const result = await this.api.get<BanchoBeatmap>(`api/v2/beatmaps/lookup`, {
config: {
headers: {
Authorization: `Bearer ${await this.osuApiKey}`,
},
params: {
filename: beatmapHash,
},
},
});

if (!result || result.status !== 200 || !result.data) {
return { result: null, status: result?.status ?? 500 };
}

return {
result: this.convertService.convertBeatmap(result.data),
status: result.status,
};
}

private get osuApiKey() {
return this.banchoService.getBanchoClientToken();
}
Expand Down
9 changes: 6 additions & 3 deletions server/src/core/managers/mirrors/mirrors.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export class MirrorsManager {
async getBeatmap(
ctx: GetBeatmapOptions,
): Promise<ResultWithStatus<Beatmap>> {
if (!ctx.beatmapId && !ctx.beatmapHash) {
throw new Error("Either beatmapId or beatmapHash is required");
if (!ctx.beatmapId && !ctx.beatmapHash && !ctx.beatmapFilename) {
throw new Error("Either beatmapId, beatmapHash or beatmapFilename is required");
}

let criteria: ClientAbilities;
Expand All @@ -135,11 +135,14 @@ export class MirrorsManager {
? ClientAbilities.GetBeatmapByIdWithSomeNonBeatmapValues
: ClientAbilities.GetBeatmapById;
}
else {
else if (ctx.beatmapHash) {
criteria = ctx.allowMissingNonBeatmapValues
? ClientAbilities.GetBeatmapByHashWithSomeNonBeatmapValues
: ClientAbilities.GetBeatmapByHash;
}
else {
criteria = ClientAbilities.GetBeatmapByFilename;
}

return await this.useMirror<Beatmap>(ctx, criteria, "getBeatmap");
}
Expand Down
43 changes: 39 additions & 4 deletions server/src/core/managers/storage/storage-cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,28 @@ export class StorageCacheService {
async getBeatmap(
ctx: GetBeatmapOptions,
): Promise<Beatmap | null | undefined> {
if (!ctx.beatmapId && !ctx.beatmapHash && !ctx.beatmapFilename) {
throw new Error("Either beatmapId, beatmapHash or beatmapFilename is required");
}

let { beatmapId } = ctx;

if (ctx.beatmapHash) {
const cachedId = await this.redis.get(
`${RedisKeys.BEATMAP_ID_BY_HASH}${ctx.beatmapHash}`,
`${RedisKeys.BEATMAP_ID_BY_HASH}${ctx.beatmapHash}`,
);

if (!cachedId)
return undefined;
if (cachedId === "null")
return null;

beatmapId = Number(cachedId);
}

if (ctx.beatmapFilename) {
const cachedId = await this.redis.get(
`${RedisKeys.BEATMAP_ID_BY_FILENAME}${ctx.beatmapFilename}`,
);

if (!cachedId)
Expand All @@ -46,9 +63,18 @@ export class StorageCacheService {
}

async insertEmptyBeatmap(ctx: GetBeatmapOptions) {
const key = ctx?.beatmapId
? `${RedisKeys.BEATMAP_BY_ID}${ctx?.beatmapId}`
: `${RedisKeys.BEATMAP_ID_BY_HASH}${ctx?.beatmapHash}`;
let key = "";

if (ctx?.beatmapId) {
key = `${RedisKeys.BEATMAP_BY_ID}${ctx?.beatmapId}`;
}
else if (ctx?.beatmapHash) {
key = `${RedisKeys.BEATMAP_ID_BY_HASH}${ctx?.beatmapHash}`;
}
else if (ctx?.beatmapFilename) {
key = `${RedisKeys.BEATMAP_ID_BY_FILENAME}${ctx?.beatmapFilename}`;
}

await this.redis.set(
key,
"null",
Expand All @@ -73,6 +99,15 @@ export class StorageCacheService {
);
}

async insertBeatmapByFilename(beatmap: Beatmap, filename: string) {
await this.redis.set(
`${RedisKeys.BEATMAP_ID_BY_FILENAME}${filename}`,
beatmap.id,
"PX",
this.getRedisTTLBasedOnStatus(beatmap.status),
);
}

async insertEmptyBeatmapset(ctx: GetBeatmapSetOptions) {
const key = `${RedisKeys.BEATMAPSET_BY_ID}${ctx?.beatmapSetId}`;
await this.redis.set(
Expand Down
11 changes: 11 additions & 0 deletions server/src/core/managers/storage/storage.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class StorageManager {
async getBeatmap(
ctx: GetBeatmapOptions,
): Promise<Beatmap | null | undefined> {
if (!ctx.beatmapId && !ctx.beatmapHash && !ctx.beatmapFilename) {
throw new Error("Either beatmapId, beatmapHash or beatmapFilename is required");
}

let entity = await this.cacheService.getBeatmap(ctx);

if (entity !== undefined) {
Expand All @@ -61,6 +65,9 @@ export class StorageManager {
else if (ctx.beatmapHash) {
entity = await getBeatmapByHash(ctx.beatmapHash);
}
else if (ctx.beatmapFilename) {
// We don't store filenames in database, skip
}

if (entity) {
this.cacheService.insertBeatmap(entity);
Expand Down Expand Up @@ -145,6 +152,10 @@ export class StorageManager {
if (beatmap) {
await createBeatmap(beatmap);
await this.cacheService.insertBeatmap(beatmap);

if (ctx.beatmapFilename) {
await this.cacheService.insertBeatmapByFilename(beatmap, ctx.beatmapFilename);
}
}
else {
await this.cacheService.insertEmptyBeatmap(ctx);
Expand Down
1 change: 1 addition & 0 deletions server/src/types/redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum RedisKeys {
BEATMAP_BY_ID = "BEATMAP:ID:",
BEATMAP_ID_BY_HASH = "BEATMAP_ID:HASH:",
BEATMAP_ID_BY_FILENAME = "BEATMAP_ID:FILENAME:",
BEATMAPSET_BY_ID = "BEATMAPSET:ID:",
BEATMAPSET_FILE_BY_ID = "BEATMAPSET_FILE:ID:",
BEATMAP_OSU_FILE = "BEATMAP_OSU_FILE:ID:",
Expand Down
Loading
Loading