-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcos Jordão
authored
Aug 24, 2022
1 parent
ce9cc9e
commit 2ec5238
Showing
12 changed files
with
309 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { getDownloadURL, getStorage, ref, StorageReference, uploadString } from 'firebase/storage' | ||
import { ImageDimensions } from 'react-canvas-draw' | ||
import Image from '../../model/image' | ||
import { getImageIndexFromName } from '../../util/image-util' | ||
import { MaskUploadResult } from './api/MaskUploadResult' | ||
|
||
export async function getImageUrl(image: Image): Promise<string> { | ||
const storage = getStorage() | ||
const reference: StorageReference = ref(storage, image.sampleLocation + '/' + image.name) | ||
return getDownloadURL(reference) | ||
} | ||
|
||
export async function uploadMaskImage(image: Image, maskImageFileContent: string): Promise<MaskUploadResult> { | ||
const storage = getStorage() | ||
|
||
const imageIndex = getImageIndexFromName(image.name) | ||
const maskIndex = image.masks?.length ?? 0 | ||
const fileName = `mask_${imageIndex}_${maskIndex}.png` | ||
|
||
const referencePath = `${image.sampleLocation}/${fileName}` | ||
const reference = ref(storage, referencePath) | ||
const uploadResult = await uploadString(reference, maskImageFileContent, 'data_url') | ||
|
||
const maskUploadResult: MaskUploadResult = { | ||
fileName: uploadResult.ref.name, | ||
fullPath: uploadResult.ref.fullPath, | ||
} | ||
|
||
return maskUploadResult | ||
} | ||
|
||
export async function getImageDimensions(url: string): Promise<ImageDimensions> { | ||
const metadata = await getMeta(url) | ||
|
||
return { | ||
width: metadata.width, | ||
height: metadata.height, | ||
} | ||
} | ||
|
||
function getMeta(url: string): Promise<HTMLImageElement> { | ||
return new Promise<HTMLImageElement>((resolve, reject) => { | ||
const img = new Image() | ||
img.onload = () => resolve(img) | ||
img.onerror = () => reject() | ||
img.src = url | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface MaskUploadResult { | ||
fileName: string | ||
fullPath: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.