feat: fix youtube title

This commit is contained in:
Nevo David 2025-08-07 21:13:38 +07:00
parent 326c1b60c6
commit 9e87928045
4 changed files with 70 additions and 78 deletions

View File

@ -142,6 +142,7 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
(type: 'draft' | 'now' | 'schedule') => async () => {
setLoading(true);
const checkAllValid = await ref.current.checkAllValid();
console.log(checkAllValid);
if (type !== 'draft') {
const notEnoughChars = checkAllValid.filter((p: any) => {
return p.values.some((a: any) => {

View File

@ -1,11 +1,5 @@
import {
IsArray,
IsDefined,
IsIn,
IsOptional,
IsString,
MinLength,
ValidateNested,
IsArray, IsDefined, IsIn, IsOptional, IsString, MaxLength, MinLength, ValidateNested
} from 'class-validator';
import { MediaDto } from '@gitroom/nestjs-libraries/dtos/media/media.dto';
import { Type } from 'class-transformer';
@ -21,6 +15,7 @@ export class YoutubeTagsSettings {
export class YoutubeSettingsDto {
@IsString()
@MinLength(2)
@MaxLength(100)
@IsDefined()
title: string;

View File

@ -50,6 +50,7 @@ export abstract class SocialAbstract {
try {
return await func();
} catch (err) {
console.log(err);
const handle = this.handleErrors(JSON.stringify(err));
return { err: true, ...(handle || {}) };
}

View File

@ -65,6 +65,55 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
editor = 'normal' as const;
override handleErrors(body: string):
| {
type: 'refresh-token' | 'bad-body';
value: string;
}
| undefined {
if (body.includes('invalidTitle')) {
return {
type: 'bad-body',
value:
'We have uploaded your video but we could not set the title. Title is too long.',
};
}
if (body.includes('failedPrecondition')) {
return {
type: 'bad-body',
value:
'We have uploaded your video but we could not set the thumbnail. Thumbnail size is too large.',
};
}
if (body.includes('uploadLimitExceeded')) {
return {
type: 'bad-body',
value:
'You have reached your daily upload limit, please try again tomorrow.',
};
}
if (body.includes('youtubeSignupRequired')) {
return {
type: 'bad-body',
value:
'You have to link your youtube account to your google account first.',
};
}
if (body.includes('youtube.thumbnail')) {
return {
type: 'bad-body',
value:
'Your account is not verified, we have uploaded your video but we could not set the thumbnail. Please verify your account and try again.',
};
}
return undefined;
}
async refreshToken(refresh_token: string): Promise<AuthTokenDetails> {
const { client, oauth2 } = clientAndYoutube();
client.setCredentials({ refresh_token });
@ -153,9 +202,8 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
responseType: 'stream',
});
let all: GaxiosResponse<Schema$Video>;
try {
all = await this.runInConcurrent(async () =>
const all: GaxiosResponse<Schema$Video> = await this.runInConcurrent(
async () =>
youtubeClient.videos.insert({
part: ['id', 'snippet', 'status'],
notifySubscribers: true,
@ -175,76 +223,23 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
body: response.data,
},
})
);
} catch (err: any) {
if (
err.response?.data?.error?.errors?.[0]?.reason === 'failedPrecondition'
) {
throw new BadBody(
'youtube',
JSON.stringify(err.response.data),
JSON.stringify(err.response.data),
'We have uploaded your video but we could not set the thumbnail. Thumbnail size is too large.'
);
}
if (
err.response?.data?.error?.errors?.[0]?.reason === 'uploadLimitExceeded'
) {
throw new BadBody(
'youtube',
JSON.stringify(err.response.data),
JSON.stringify(err.response.data),
'You have reached your daily upload limit, please try again tomorrow.'
);
}
if (
err.response?.data?.error?.errors?.[0]?.reason ===
'youtubeSignupRequired'
) {
throw new BadBody(
'youtube',
JSON.stringify(err.response.data),
JSON.stringify(err.response.data),
'You have to link your youtube account to your google account first.'
);
}
throw new BadBody(
'youtube',
JSON.stringify(err.response.data),
JSON.stringify(err.response.data),
'An error occurred while uploading your video, please try again later.'
);
}
);
if (settings?.thumbnail?.path) {
try {
await this.runInConcurrent(async () =>
youtubeClient.thumbnails.set({
videoId: all?.data?.id!,
media: {
body: (
await axios({
url: settings?.thumbnail?.path,
method: 'GET',
responseType: 'stream',
})
).data,
},
})
);
} catch (err: any) {
if (
err.response?.data?.error?.errors?.[0]?.domain === 'youtube.thumbnail'
) {
throw new BadBody(
'',
JSON.stringify(err.response.data),
JSON.stringify(err.response.data),
'Your account is not verified, we have uploaded your video but we could not set the thumbnail. Please verify your account and try again.'
);
}
}
await this.runInConcurrent(async () =>
youtubeClient.thumbnails.set({
videoId: all?.data?.id!,
media: {
body: (
await axios({
url: settings?.thumbnail?.path,
method: 'GET',
responseType: 'stream',
})
).data,
},
})
);
}
return [