Skip to content

Commit

Permalink
fix: use URL type for media location and thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Feb 2, 2025
1 parent 9b4d84f commit 429544f
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 131 deletions.
8 changes: 4 additions & 4 deletions packages/@liexp/backend/src/entities/Event.v2.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type BlockNoteDocument } from "@liexp/shared/lib/io/http/Common/BlockNoteDocument.js";
import { type UUID } from "@liexp/shared/lib/io/http/Common/index.js";
import { UNCATEGORIZED } from "@liexp/shared/lib/io/http/Events/EventType.js";
import * as http from "@liexp/shared/lib/io/http/index.js";
import { type BNEditorDocument } from "@liexp/shared/lib/providers/blocknote/type.js";
import {
Column,
CreateDateColumn,
Expand All @@ -10,10 +10,10 @@ import {
Index,
JoinTable,
ManyToMany,
ManyToOne,
PrimaryGeneratedColumn,
type Relation,
UpdateDateColumn,
ManyToOne,
} from "typeorm";
import { type ActorEntity } from "./Actor.entity.js";
import { AreaEntity } from "./Area.entity.js";
Expand All @@ -39,10 +39,10 @@ export class EventV2Entity {
date: Date;

@Column({ type: "json", nullable: true })
excerpt: BNEditorDocument | null;
excerpt: BlockNoteDocument | null;

@Column({ type: "json", nullable: true })
body: any[] | null;
body: BlockNoteDocument | null;

@Column({
type: "enum",
Expand Down
6 changes: 3 additions & 3 deletions packages/@liexp/backend/src/entities/Media.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type UUID } from "@liexp/shared/lib/io/http/Common/index.js";
import { type URL, type UUID } from "@liexp/shared/lib/io/http/Common/index.js";
import {
MediaType,
type MediaExtra,
Expand Down Expand Up @@ -35,10 +35,10 @@ export class MediaEntity {
label: string | null;

@Column({ type: "varchar", nullable: true })
thumbnail: string | null;
thumbnail: URL | null;

@Column({ type: "varchar", nullable: false, unique: true })
location: string;
location: URL;

@Column({ type: "varchar", nullable: true })
description: string | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { PngType } from "@liexp/shared/lib/io/http/Media/index.js";
import {
contentTypeFromFileExt,
Expand Down Expand Up @@ -48,8 +49,8 @@ const uploadScreenshot = <C extends SpaceContext & ENVContext>(
links: [],
areas: [],
type: PngType.value,
location: upload.Location,
thumbnail: upload.Location,
location: upload.Location as URL,
thumbnail: upload.Location as URL,
})),
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";
import { type ListObjectsOutput, type _Object } from "@aws-sdk/client-s3";
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { walkPaginatedRequest } from "@liexp/shared/lib/utils/fp.utils.js";
import { getResourceAndIdFromLocation } from "@liexp/shared/lib/utils/media.utils.js";
import { type Option } from "fp-ts/lib/Option.js";
Expand Down Expand Up @@ -94,7 +95,7 @@ export const getOrphanMediaFlow = <
return ctx.db.findOne(MediaEntity, {
where: [
{
location: Like(`%${e.Key}`),
location: Like(`%${e.Key}` as URL),
},
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { type UUID, uuid } from "@liexp/shared/lib/io/http/Common/UUID.js";
import {
IframeVideoType,
Expand Down Expand Up @@ -58,7 +59,7 @@ export const createAndUpload = <C extends CreateAndUploadFlowContext>(
// ctx.logger.debug.log("Create media and upload %s", createMediaData);

if (IframeVideoType.is(createMediaData.type)) {
return fp.RTE.right(createMediaData.location);
return fp.RTE.right(createMediaData.location as URL);
}

const mediaKey = getMediaKey(
Expand All @@ -74,7 +75,7 @@ export const createAndUpload = <C extends CreateAndUploadFlowContext>(
ContentType,
ACL: "public-read",
}),
fp.RTE.map((r) => r.Location),
fp.RTE.map((r) => r.Location as URL),
fp.RTE.mapLeft(ServerError.fromUnknown),
);
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const extractEmbedFromPlatform = (
url: URL,
m: VideoPlatformMatch,
page: puppeteer.Page,
): TE.TaskEither<ServerError, string> => {
): TE.TaskEither<ServerError, URL> => {
return pipe(
TE.tryCatch(async () => {
await page.goto(url);
Expand Down Expand Up @@ -104,7 +104,7 @@ export const extractMediaFromPlatform =
thumbnail: pipe(
extractThumbnailFromVideoPlatform(m, page)(ctx),
TE.orElse(
(): TE.TaskEither<ServerError, string | undefined> =>
(): TE.TaskEither<ServerError, URL | undefined> =>
TE.right(undefined),
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { uuid } from "@liexp/shared/lib/io/http/Common/UUID.js";
import { type Option } from "fp-ts/lib/Option.js";
import { type ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither.js";
Expand All @@ -11,12 +12,12 @@ import { type DatabaseContext } from "context/db.context.js";
export const findOneByLocationOrElse =
<C extends DatabaseContext>(
m: Partial<Metadata>,
orElse: (m: string) => Partial<MediaEntity>,
orElse: (m: URL) => Partial<MediaEntity>,
creator: UserEntity,
): ReaderTaskEither<C, DBError, Option<MediaEntity>> =>
(ctx) => {
return pipe(
m.image,
m.image as URL,
fp.O.fromNullable,
fp.O.map((image) =>
pipe(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type PutObjectCommandInput } from "@aws-sdk/client-s3";
import { fp, pipe } from "@liexp/core/lib/fp/index.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { type ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither.js";
import { type ConfigContext } from "../../../context/config.context.js";
import { type ENVContext } from "../../../context/env.context.js";
Expand All @@ -18,7 +19,7 @@ import { extractThumbnail } from "./extractThumbnail.flow.js";

const uploadThumbnails = <C extends SpaceContext & ENVContext>(
thumbnails: PutObjectCommandInput[],
): ReaderTaskEither<C, SpaceError, string[]> =>
): ReaderTaskEither<C, SpaceError, URL[]> =>
pipe(
thumbnails,
fp.A.traverse(fp.RTE.ApplicativePar)((thumbnail) =>
Expand All @@ -27,7 +28,7 @@ const uploadThumbnails = <C extends SpaceContext & ENVContext>(
...thumbnail,
ACL: "public-read",
}),
fp.RTE.map(({ Location }) => Location),
fp.RTE.map(({ Location }) => Location as URL),
),
),
);
Expand All @@ -48,6 +49,6 @@ export const createThumbnail = <
SpaceContext,
>(
media: SimpleMedia,
): ReaderTaskEither<C, SpaceError, string[]> => {
): ReaderTaskEither<C, SpaceError, URL[]> => {
return pipe(extractThumbnail<C>(media), fp.RTE.chain(uploadThumbnails<C>));
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getPlatform,
type VideoPlatformMatch,
} from "@liexp/shared/lib/helpers/media.helper.js";
import { type URL } from "@liexp/shared/lib/io/http/Common/URL.js";
import { type Media } from "@liexp/shared/lib/io/http/index.js";
import * as E from "fp-ts/lib/Either.js";
import { type ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither.js";
Expand All @@ -21,7 +22,7 @@ export const extractThumbnailFromVideoPlatform =
<C extends LoggerContext>(
match: VideoPlatformMatch,
page: Page,
): ReaderTaskEither<C, ServerError, string> =>
): ReaderTaskEither<C, ServerError, URL> =>
(ctx) => {
const toError = (m: VideoPlatformMatch): ServerError => {
return ServerError.fromUnknown(
Expand Down Expand Up @@ -50,7 +51,7 @@ export const extractThumbnailFromVideoPlatform =
`Thumbnail from selector ${selector}: ${coverUrl}`,
);

return coverUrl;
return coverUrl as URL;
}
case "youtube": {
const selector = 'div[class*="thumbnail-overlay-image"]';
Expand All @@ -68,7 +69,7 @@ export const extractThumbnailFromVideoPlatform =
return undefined;
});

return coverUrl;
return coverUrl as URL;
}
case "peertube":
case "odysee": {
Expand All @@ -90,7 +91,7 @@ export const extractThumbnailFromVideoPlatform =
?.replace('background-image: url("', "")
.replace('");', "");

return coverUrl;
return coverUrl as URL;
});
}
case "rumble": {
Expand All @@ -111,7 +112,7 @@ export const extractThumbnailFromVideoPlatform =
return el.getAttribute("poster");
});

return videoPosterSrc;
return videoPosterSrc as URL;
}
case "dailymotion": {
if (match.type === "embed") {
Expand All @@ -121,7 +122,7 @@ export const extractThumbnailFromVideoPlatform =

return page.$eval(selector, (el) => {
return el.getAttribute("src");
});
}) as Promise<URL | null>;
}

const selector = 'meta[name="og:image:secure_url"]';
Expand All @@ -131,10 +132,10 @@ export const extractThumbnailFromVideoPlatform =
return el.getAttribute("content");
});

return url;
return url as URL;
}
default: {
return undefined;
return null;
}
}
}, ServerError.fromUnknown),
Expand Down
2 changes: 1 addition & 1 deletion packages/@liexp/backend/src/flows/tg/parseVideo.flow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pipe } from "@liexp/core/lib/fp/index.js";
import { uuid, type UUID } from "@liexp/shared/lib/io/http/Common/UUID.js";
import { MP4Type } from "@liexp/shared/lib/io/http/Media/index.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/media.utils.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/url.utils.js";
import { sequenceS } from "fp-ts/lib/Apply.js";
import * as O from "fp-ts/lib/Option.js";
import { type ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither.js";
Expand Down
17 changes: 9 additions & 8 deletions packages/@liexp/backend/src/flows/tg/upsertPinnedMessage.flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { sequenceS } from "fp-ts/lib/Apply.js";
import { type ReaderTaskEither } from "fp-ts/lib/ReaderTaskEither.js";
import * as TE from "fp-ts/lib/TaskEither.js";
import type TelegramBot from "node-telegram-bot-api";
import { type DatabaseContext } from "../../context/db.context";
import { type ENVContext } from "../../context/env.context";
import { type DatabaseContext } from "../../context/db.context.js";
import { type ENVContext } from "../../context/env.context.js";
import { type TGBotProviderContext } from "../../context/index.js";
import { type LoggerContext } from "../../context/logger.context";
import { EventV2Entity } from "../../entities/Event.v2.entity";
import { KeywordEntity } from "../../entities/Keyword.entity";
import { type LoggerContext } from "../../context/logger.context.js";
import { EventV2Entity } from "../../entities/Event.v2.entity.js";
import { KeywordEntity } from "../../entities/Keyword.entity.js";
import { type TG_BOT_ENV } from "../../io/ENV.js";
import { type DBError } from "../../providers/orm/index.js";
import { type TGError, toTGError } from "../../providers/tg/tg.provider";
import { LoggerService } from "../../services/logger/logger.service";
import { type TGError, toTGError } from "../../providers/tg/tg.provider.js";
import { LoggerService } from "../../services/logger/logger.service.js";

interface ToPinnedMessageOptions {
bot: string;
Expand Down Expand Up @@ -40,7 +41,7 @@ ${keywords.map((k) => `#${k.tag} (${k.eventCount})`).join("\n")}

export type UpsertPinnerMessageFlowContext = DatabaseContext &
LoggerContext &
ENVContext &
ENVContext<TG_BOT_ENV> &
TGBotProviderContext;

export const upsertPinnedMessage =
Expand Down
10 changes: 9 additions & 1 deletion packages/@liexp/backend/src/io/ENV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { NODE_ENV } from "@liexp/core/lib/env/node-env.js";
import * as t from "io-ts";
import { NumberFromString } from "io-ts-types/lib/NumberFromString.js";

export const JWT_ENV = t.strict(
{
JWT_SECRET: t.string,
},
"JWT_ENV",
);

export type JWT_ENV = t.TypeOf<typeof JWT_ENV>;

export const DATABASE_ENV = t.intersection(
[
t.strict(
Expand Down Expand Up @@ -58,7 +67,6 @@ export const BACKEND_ENV = t.intersection(
t.strict({ NODE_ENV, DEFAULT_PAGE_SIZE: NumberFromString }),
DATABASE_ENV,
SPACE_ENV,
TG_BOT_ENV,
],
"BACKEND_ENV",
);
Expand Down
2 changes: 1 addition & 1 deletion packages/@liexp/backend/src/io/media.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { MediaExtraMonoid } from "@liexp/shared/lib/io/http/Media/MediaExtra.js";
import { type MediaType } from "@liexp/shared/lib/io/http/Media/MediaType.js";
import * as io from "@liexp/shared/lib/io/index.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/media.utils.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/url.utils.js";
import * as E from "fp-ts/lib/Either.js";
import { type MediaEntity } from "../entities/Media.entity.js";
import { IOCodec } from "./DomainCodec.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pipe, fp } from "@liexp/core/lib/fp/index.js";
import { type Logger } from "@liexp/core/lib/logger/index.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/media.utils.js";
import { ensureHTTPS } from "@liexp/shared/lib/utils/url.utils.js";
import {
type AxiosRequestConfig,
type AxiosInstance,
Expand Down
22 changes: 2 additions & 20 deletions packages/@liexp/shared/src/endpoints/media.endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as t from "io-ts";
import { UUID } from "io-ts-types/lib/UUID.js";
import { optionFromNullable } from "io-ts-types/lib/optionFromNullable.js";
import { Endpoint } from "ts-endpoint";
import { ListOutput, Output } from "../io/http/Common/Output.js";
import { CreateMedia, MediaExtra } from "../io/http/Media/index.js";
import { CreateMedia, EditMediaBody } from "../io/http/Media/index.js";
import { Media } from "../io/http/index.js";
import { ResourceEndpoints } from "./types.js";

Expand Down Expand Up @@ -43,24 +42,7 @@ export const Edit = Endpoint({
getPath: ({ id }) => `/media/${id}`,
Input: {
Params: t.type({ id: UUID }),
Body: t.strict({
type: Media.MediaType,
thumbnail: optionFromNullable(t.string),
location: t.string,
label: t.string,
description: optionFromNullable(t.string),
extra: optionFromNullable(MediaExtra),
links: t.array(UUID),
events: t.array(UUID),
keywords: t.array(UUID),
areas: t.array(UUID),
creator: optionFromNullable(UUID),
overrideThumbnail: optionFromNullable(t.boolean),
overrideExtra: optionFromNullable(t.boolean),
transfer: optionFromNullable(t.boolean),
transferThumbnail: optionFromNullable(t.boolean),
restore: optionFromNullable(t.boolean),
}),
Body: EditMediaBody,
},
Output: SingleMediaOutput,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@liexp/shared/src/io/http/Events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const CreateEventPlainBody = t.union(
export type CreateEventPlainBody = t.TypeOf<typeof CreateEventPlainBody>;

export const CreateEventBody = t.union(
[CreateEventPlainBody, EventFromURLBody],
[EventFromURLBody, CreateEventPlainBody],
"CreateEventBody",
);

Expand Down
4 changes: 2 additions & 2 deletions packages/@liexp/shared/src/io/http/Media/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export type CreateMedia = t.TypeOf<typeof CreateMedia>;
export const EditMediaBody = t.strict(
{
type: MediaType,
thumbnail: optionFromNullable(t.string),
location: t.string,
thumbnail: optionFromNullable(URL),
location: URL,
label: t.string,
description: optionFromNullable(t.string),
extra: optionFromNullable(MediaExtra),
Expand Down
Loading

0 comments on commit 429544f

Please sign in to comment.