Skip to content

Commit

Permalink
Generate and display S3 URls.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaymavi committed Dec 4, 2023
1 parent 8c5b1c0 commit e2d603b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/auth/slides/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import { ServerClient } from "@/src/server-client";
import {formatDateString} from "@/src/helpers";

const Slides: FC = () => {
const { listPresentations } = useSlidesStore();
const { listPresentations,getS3PublicUrl } = useSlidesStore();
const currentProject = useSlidesStore((store) => store.currentProject);
const presentations = useSlidesStore((store) => store.presentations);
const apiServer = useSlidesStore((store) => store.apiServer);
const batchApiServer =useSlidesStore((store) => store.batchApiServer);
const s3PublicUrls = useSlidesStore((store) => store.s3PublicUrls);

const refreshList = () => {
if (currentProject) {
Expand Down Expand Up @@ -107,8 +108,17 @@ const Slides: FC = () => {
return <Subtitle2>Video</Subtitle2>;
},
renderCell: (item) => {
return 'TODO'
}
const url =item.s3VideoFile && s3PublicUrls?.[item.s3VideoFile];
return url ? <Link target="_blank" href={url}>Download file</Link> : <Button
disabled={!item.isVideoGenerated}
appearance="outline"
onClick={() => {
getS3PublicUrl(item.s3VideoFile as string)
}}
>
Download
</Button>
}
}),
createTableColumn<IPresentation>({
columnId: "link",
Expand Down
10 changes: 10 additions & 0 deletions src/server-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ export class ServerClient {
const url = `${apiServer}/video/generate`;
ServerClient.send(url, {...presentation,url:presentationUrl, pid:presentation.id}, HttpMethod.POST);
}

// generate S3 get signed url
public static async generateS3GetSignedUrl(
apiServer: string,
key:string,
) {
const url = `${apiServer}/auth/downloadS3ObjUrl?key=${key}`;
const resp = await ServerClient.send(url, undefined, HttpMethod.GET);
return await resp.json();
}
}
24 changes: 24 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ export const useSlidesStore = create<IStore>()(
fullPresentations: fullPresentations,
});
},
getS3PublicUrl:async (key: string) =>{
const store = get();
const resp:{url:string} = await ServerClient.generateS3GetSignedUrl(store.apiServer as string,key);

let s3PublicUrls = store.s3PublicUrls ? store.s3PublicUrls : {};
s3PublicUrls[key] = resp.url;
set({
...store,
s3PublicUrls: s3PublicUrls,
});
setTimeout(()=>{
store.removeS3PublicUrl(key);
},3600 * 1000 )
},
// remove single S3 public url from store
removeS3PublicUrl:async (key: string) =>{
const store = get();
let s3PublicUrls = store.s3PublicUrls ? store.s3PublicUrls : {};
delete s3PublicUrls[key];
set({
...store,
s3PublicUrls: s3PublicUrls,
});
},
}),
{
name: "slide-storage",
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ export interface IStore {
createSlide?: {
editorFile: any;
};
s3PublicUrls?:Record<string, string>
addServer(server: string, batchApiServer:string): void;
addEditorFile(content: any): void;
addUser(user: IUserWithProjectTypes): void;
listPresentations(projectId: string): void;
getPresentation(pid: string, updateAt: string, apiKey?: string): void;
getS3PublicUrl(key:string):void;
removeS3PublicUrl(key:string):void;
removeS3PublicUrl(key:string):void;
}

interface exoportDefault {
Expand Down

0 comments on commit e2d603b

Please sign in to comment.