Skip to content

Commit

Permalink
feat: fix images have no path
Browse files Browse the repository at this point in the history
  • Loading branch information
nevo-david committed Jan 7, 2025
1 parent b13a862 commit 1969226
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 31 deletions.
8 changes: 4 additions & 4 deletions apps/frontend/src/components/media/media.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,14 @@ export const MultiMediaComponent: FC<{
<div className="cursor-pointer w-[40px] h-[40px] border-2 border-tableBorder relative flex">
<div
className="w-full h-full"
onClick={() => window.open(mediaDirectory.set(media.path))}
onClick={() => window.open(mediaDirectory.set(media?.path))}
>
{media.path.indexOf('mp4') > -1 ? (
<VideoFrame url={mediaDirectory.set(media.path)} />
{media?.path?.indexOf('mp4') > -1 ? (
<VideoFrame url={mediaDirectory.set(media?.path)} />
) : (
<img
className="w-full h-full object-cover"
src={mediaDirectory.set(media.path)}
src={mediaDirectory.set(media?.path)}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export class MediaRepository {
});
}

getMediaById(id: string) {
return this._media.model.media.findUnique({
where: {
id,
},
});
}

deleteMedia(org: string, id: string) {
return this._media.model.media.update({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class MediaService {
return this._mediaRepository.deleteMedia(org, id);
}

getMediaById(id: string) {
return this._mediaRepository.getMediaById(id);
}

async generateImage(
prompt: string,
org: Organization,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ export class PostsRepository {
});
}

updateImages(id: string, images: string) {
return this._post.model.post.update({
where: {
id,
},
data: {
image: images,
},
});
}

getPostUrls(orgId: string, ids: string[]) {
return this._post.model.post.findMany({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { BullMqClient } from '@gitroom/nestjs-libraries/bull-mq-transport-new/cl
import { timer } from '@gitroom/helpers/utils/timer';
import { AuthTokenDetails } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
import utc from 'dayjs/plugin/utc';
import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';
dayjs.extend(utc);

type PostWithConditionals = Post & {
Expand All @@ -36,7 +37,8 @@ export class PostsService {
private _notificationService: NotificationService,
private _messagesService: MessagesService,
private _stripeService: StripeService,
private _integrationService: IntegrationService
private _integrationService: IntegrationService,
private _mediaService: MediaService
) {}

async getPostsRecursively(
Expand Down Expand Up @@ -73,18 +75,63 @@ export class PostsService {
return this._postRepository.getPosts(orgId, query);
}

async updateMedia(id: string, imagesList: any[]) {
let imageUpdateNeeded = false;
const getImageList = (
await Promise.all(
imagesList.map(async (p: any) => {
if (!p.path && p.id) {
imageUpdateNeeded = true;
return this._mediaService.getMediaById(p.id);
}

return p;
})
)
).map((m) => {
return {
...m,
url:
m.path.indexOf('http') === -1
? process.env.FRONTEND_URL +
'/' +
process.env.NEXT_PUBLIC_UPLOAD_STATIC_DIRECTORY +
m.path
: m.path,
type: 'image',
path:
m.path.indexOf('http') === -1
? process.env.UPLOAD_DIRECTORY + m.path
: m.path,
};
});

if (imageUpdateNeeded) {
await this._postRepository.updateImages(id, JSON.stringify(getImageList));
}

return getImageList;
}

async getPost(orgId: string, id: string) {
const posts = await this.getPostsRecursively(id, true, orgId, true);
return {
const list = {
group: posts?.[0]?.group,
posts: posts.map((post) => ({
...post,
image: JSON.parse(post.image || '[]'),
})),
posts: await Promise.all(
posts.map(async (post) => ({
...post,
image: await this.updateMedia(
post.id,
JSON.parse(post.image || '[]')
),
}))
),
integrationPicture: posts[0]?.integration?.picture,
integration: posts[0].integrationId,
settings: JSON.parse(posts[0].settings || '{}'),
};

return list;
}

async getOldPosts(orgId: string, date: string) {
Expand Down Expand Up @@ -280,25 +327,14 @@ export class PostsService {
const publishedPosts = await getIntegration.post(
integration.internalId,
integration.token,
newPosts.map((p) => ({
id: p.id,
message: p.content,
settings: JSON.parse(p.settings || '{}'),
media: (JSON.parse(p.image || '[]') as Media[]).map((m) => ({
url:
m.path.indexOf('http') === -1
? process.env.FRONTEND_URL +
'/' +
process.env.NEXT_PUBLIC_UPLOAD_STATIC_DIRECTORY +
m.path
: m.path,
type: 'image',
path:
m.path.indexOf('http') === -1
? process.env.UPLOAD_DIRECTORY + m.path
: m.path,
})),
})),
await Promise.all(
newPosts.map(async (p) => ({
id: p.id,
message: p.content,
settings: JSON.parse(p.settings || '{}'),
media: await this.updateMedia(p.id, JSON.parse(p.image || '[]')),
}))
),
integration
);

Expand Down Expand Up @@ -802,7 +838,12 @@ export class PostsService {
return this._postRepository.getComments(postId);
}

createComment(orgId: string, userId: string, postId: string, comment: string) {
createComment(
orgId: string,
userId: string,
postId: string,
comment: string
) {
return this._postRepository.createComment(orgId, userId, postId, comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const VideoOrImage: FC<{
isContain?: boolean;
}> = (props) => {
const { src, autoplay, isContain } = props;
if (src.indexOf('mp4') > -1) {
if (src?.indexOf('mp4') > -1) {
return (
<video
src={src}
Expand Down

0 comments on commit 1969226

Please sign in to comment.