Skip to content

Commit

Permalink
fix: download file with same name it was uploaded (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlaPaiva authored Jan 14, 2024
1 parent 58fefb0 commit 1792029
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/components/converter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import DraggerUpload from "../dragger-upload"
import { Button, UploadFile } from "antd"
import convertFiles from "../../services/converter"
import { Format } from "../../model/format"
import './converter.css'
import { ConvertedFile } from "../../model/converted-file"

import './converter.css'
/** Properties of Converter */
type ConverterProps = {
/** Define allowed types to be uploaded */
Expand All @@ -20,14 +21,14 @@ function Converter(props: ConverterProps): JSX.Element {
setFileList(files)
}, [])

const downloadBlob = useCallback((blob: Blob) => {
const url = window.URL.createObjectURL(blob);
const downloadBlob = useCallback((convertedFile: ConvertedFile) => {
const url = window.URL.createObjectURL(convertedFile.blob);
const a = document.createElement("a")
document.body.appendChild(a)
a.style.display = "none"
a.href = url;
a.target = "_blank"
a.download = 'test.gif';
a.download = convertedFile.newFileName;
a.click();
window.URL.revokeObjectURL(url);
}, [])
Expand Down
5 changes: 5 additions & 0 deletions src/model/converted-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ConvertedFile = {
newFileName: string,
blob: Blob,
result: 'success' | 'error'
}
12 changes: 9 additions & 3 deletions src/services/converter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createFFmpeg } from "@ffmpeg/ffmpeg";
import { UploadFile } from "antd";
import { ConvertedFile } from "../model/converted-file";

async function convertFiles(fileList: UploadFile[]): Promise<Blob[]>{
const blobList: Blob[] = []
async function convertFiles(fileList: UploadFile[]): Promise<ConvertedFile[]>{
const blobList: ConvertedFile[] = []

for (const file of fileList) {
const sourceBuffer = await file.originFileObj?.arrayBuffer() as ArrayBuffer
Expand All @@ -22,7 +23,12 @@ async function convertFiles(fileList: UploadFile[]): Promise<Blob[]>{
await ffmpeg.run("-i", file.name, outputName);

const output = ffmpeg.FS("readFile", outputName);
blobList.push(new Blob([output.buffer], { type: "image/gif" }))
const blob = new Blob([output.buffer], { type: "image/gif" })
blobList.push({
newFileName: outputName,
blob,
result: "success"
})
}

return blobList
Expand Down

0 comments on commit 1792029

Please sign in to comment.