Skip to content

Commit

Permalink
chore: transfer hotpatches
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryoverload committed Jan 16, 2025
1 parent 0cd9b63 commit 233a826
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const config: Config = {
connection_string: process.env.PN_MIIVERSE_API_CONFIG_MONGO_CONNECTION_STRING || '',
options: mongooseConnectOptionsMain
},
cdn_url: process.env.PN_MIIVERSE_API_CONFIG_CDN_URL || '',
s3: {
endpoint: process.env.PN_MIIVERSE_API_CONFIG_S3_ENDPOINT || '',
key: process.env.PN_MIIVERSE_API_CONFIG_S3_ACCESS_KEY || '',
Expand Down Expand Up @@ -64,6 +65,21 @@ if (!config.mongoose.connection_string) {
process.exit(0);
}

if (!config.cdn_url) {
LOG_ERROR('Failed to find CDN url. Set the PN_MIIVERSE_API_CONFIG_CDN_URL environment variable');
process.exit(0);
}

try {
new URL(config.cdn_url);
} catch (e) {
LOG_ERROR('Invalid CDN URL, URL must be a valid URL with a protocol (http/https) and domain');
process.exit(0);
}

// Remove trailing slash from CDN URL
config.cdn_url = config.cdn_url.replace(/\/$/, '');

if (!config.s3.endpoint) {
LOG_ERROR('Failed to find s3 endpoint. Set the PN_MIIVERSE_API_CONFIG_S3_ENDPOINT environment variable');
process.exit(0);
Expand Down
3 changes: 2 additions & 1 deletion src/services/api/routes/friend_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { LOG_WARN } from '@/logger';
import { Post } from '@/models/post';
import { Conversation } from '@/models/conversation';
import { FormattedMessage } from '@/types/common/formatted-message';
import { config } from '@/config-manager';

const sendMessageSchema = z.object({
body: z.string().optional(),
Expand Down Expand Up @@ -195,7 +196,7 @@ router.post('/', upload.none(), async function (request: express.Request, respon
is_app_jumpable: request.body.is_app_jumpable,
language_id: request.body.language_id,
mii: sender.mii.data,
mii_face_url: `https://mii.olv.pretendo.cc/mii/${sender.pid}/${miiFace}`,
mii_face_url: `${config.cdn_url}/mii/${sender.pid}/${miiFace}`,
pid: request.pid,
platform_id: request.paramPack.platform_id,
region_id: request.paramPack.region_id,
Expand Down
5 changes: 3 additions & 2 deletions src/services/api/routes/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Post } from '@/models/post';
import { Community } from '@/models/community';
import { HydratedPostDocument } from '@/types/mongoose/post';
import { PostRepliesResult } from '@/types/miiverse/post';
import { config } from '@/config-manager';

const newPostSchema = z.object({
community_id: z.string().optional(),
Expand Down Expand Up @@ -218,7 +219,7 @@ async function newPost(request: express.Request, response: express.Response): Pr
let user: GetUserDataResponse;

try {
user = await getUserAccountData(request.pid);
user = await getUserAccountData(request.pid);
} catch (error) {
// TODO - Log this error
response.sendStatus(403);
Expand Down Expand Up @@ -364,7 +365,7 @@ async function newPost(request: express.Request, response: express.Response): Pr
is_app_jumpable: (jumpable) ? 1 : 0,
language_id: languageID,
mii: user.mii.data,
mii_face_url: `https://mii.olv.pretendo.cc/mii/${user.pid}/${miiFace}`,
mii_face_url: `${config.cdn_url}/mii/${user.pid}/${miiFace}`,
pid: request.pid,
platform_id: platformID,
region_id: regionID,
Expand Down
1 change: 1 addition & 0 deletions src/types/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Config {
connection_string: string;
options: mongoose.ConnectOptions;
};
cdn_url: string;
s3: {
endpoint: string;
key: string;
Expand Down
10 changes: 8 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,21 @@ export function processPainting(painting: string): Buffer | null {
}
}

export async function uploadCDNAsset(key: string, data: Buffer, acl: string): Promise<void> {
export async function uploadCDNAsset(key: string, data: Buffer, acl: string): Promise<boolean> {
const awsPutParams = {
Body: data,
Key: key,
Bucket: config.s3.bucket,
ACL: acl
};

await s3.putObject(awsPutParams).promise();
try {
await s3.putObject(awsPutParams).promise();
return true;
} catch (e) {
console.error(e);
return false;
}
}

export async function getUserFriendPIDs(pid: number): Promise<number[]> {
Expand Down

0 comments on commit 233a826

Please sign in to comment.