fix: use uploadFile for single files to support long filenames and reafactor for linter (#228)
This commit is contained in:
committed by
Bálint Ujvári
parent
a56a5c3ed8
commit
db52e4471a
+32
-14
@@ -42,6 +42,28 @@ export function Share(): ReactElement {
|
|||||||
|
|
||||||
const isMountedRef = useRef(true)
|
const isMountedRef = useRef(true)
|
||||||
|
|
||||||
|
function applyFallbackMetadata(entries: Record<string, string>, indexDocument: string | null) {
|
||||||
|
const count = Object.keys(entries).length
|
||||||
|
const isVideo = Boolean(indexDocument && /.*\.(mp4|webm|ogv)$/i.test(indexDocument))
|
||||||
|
const isAudio = Boolean(indexDocument && /.*\.(mp3|ogg|oga|wav|webm|m4a|aac|flac)$/i.test(indexDocument))
|
||||||
|
const isImage = Boolean(indexDocument && /.*\.(jpg|jpeg|png|gif|webp|svg)$/i.test(indexDocument))
|
||||||
|
|
||||||
|
if (isImage || isVideo || isAudio) {
|
||||||
|
setPreview(`${apiUrl}/bzz/${hash}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
setMetadata({
|
||||||
|
hash,
|
||||||
|
type: count > 1 ? 'folder' : 'unknown',
|
||||||
|
name: indexDocument || hash || '',
|
||||||
|
count,
|
||||||
|
isWebsite: Boolean(indexDocument && /.*\.html?$/i.test(indexDocument)),
|
||||||
|
isVideo,
|
||||||
|
isAudio,
|
||||||
|
isImage,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async function prepare() {
|
async function prepare() {
|
||||||
if (!beeApi || !status.all || !hash) {
|
if (!beeApi || !status.all || !hash) {
|
||||||
return
|
return
|
||||||
@@ -79,21 +101,8 @@ export function Share(): ReactElement {
|
|||||||
setMetadata({ ...formattedMetadata, hash })
|
setMetadata({ ...formattedMetadata, hash })
|
||||||
} catch {
|
} catch {
|
||||||
// if metadata is not available or invalid go with the default one
|
// if metadata is not available or invalid go with the default one
|
||||||
const count = Object.keys(entries).length
|
|
||||||
|
|
||||||
if (!isMountedRef.current) return
|
if (!isMountedRef.current) return
|
||||||
|
applyFallbackMetadata(entries, indexDocument)
|
||||||
setMetadata({
|
|
||||||
hash,
|
|
||||||
type: count > 1 ? 'folder' : 'unknown',
|
|
||||||
name: hash,
|
|
||||||
count,
|
|
||||||
isWebsite: Boolean(indexDocument && /.*\.html?$/i.test(indexDocument)),
|
|
||||||
isVideo: Boolean(indexDocument && /.*\.(mp4|webm|ogv)$/i.test(indexDocument)),
|
|
||||||
isAudio: Boolean(indexDocument && /.*\.(mp3|ogg|oga|wav|webm|m4a|aac|flac)$/i.test(indexDocument)),
|
|
||||||
isImage: Boolean(indexDocument && /.*\.(jpg|jpeg|png|gif|webp|svg)$/i.test(indexDocument)),
|
|
||||||
// naive assumption based on indexDocument, we don't want to download the whole manifest
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
if (!isMountedRef.current) return
|
if (!isMountedRef.current) return
|
||||||
@@ -106,8 +115,17 @@ export function Share(): ReactElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onOpen() {
|
function onOpen() {
|
||||||
|
if (metadata?.isImage) {
|
||||||
|
const imgUrl = `${apiUrl}/bzz/${hash}`
|
||||||
|
const safeName = (metadata?.name ?? hash).replace(/[&<>"']/g, c => `&#${c.charCodeAt(0)};`)
|
||||||
|
const html = `<!DOCTYPE html><html><head><title>${safeName}</title></head><body style="margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center"><img src="${imgUrl}" alt="${safeName}" style="max-width:100%;max-height:100vh;object-fit:contain"></body></html>`
|
||||||
|
const blob = new Blob([html], { type: 'text/html' })
|
||||||
|
const blobUrl = URL.createObjectURL(blob)
|
||||||
|
window.open(blobUrl, '_blank', 'noopener,noreferrer')
|
||||||
|
} else {
|
||||||
window.open(`${apiUrl}/bzz/${hash}/`, '_blank', 'noopener,noreferrer')
|
window.open(`${apiUrl}/bzz/${hash}/`, '_blank', 'noopener,noreferrer')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function onClose() {
|
function onClose() {
|
||||||
if (navigate.length > 0) {
|
if (navigate.length > 0) {
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ export function Upload(): ReactElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const lastModified = files[0].lastModified
|
const lastModified = files[0].lastModified
|
||||||
|
const isSingleFile = files.length === 1
|
||||||
|
|
||||||
const metafile = new File([JSON.stringify(metadata)], META_FILE_NAME, {
|
const metafile = new File([JSON.stringify(metadata)], META_FILE_NAME, {
|
||||||
type: 'application/json',
|
type: 'application/json',
|
||||||
@@ -114,8 +115,11 @@ export function Upload(): ReactElement {
|
|||||||
|
|
||||||
await waitUntilStampUsable(stamp.batchID, beeApi)
|
await waitUntilStampUsable(stamp.batchID, beeApi)
|
||||||
|
|
||||||
beeApi
|
const uploadPromise = isSingleFile
|
||||||
.uploadFiles(stamp.batchID, fls, { indexDocument, deferred: true })
|
? beeApi.uploadFile(stamp.batchID, fls[0], fls[0].name, { deferred: true })
|
||||||
|
: beeApi.uploadFiles(stamp.batchID, fls, { indexDocument, deferred: true })
|
||||||
|
|
||||||
|
uploadPromise
|
||||||
.then(hash => {
|
.then(hash => {
|
||||||
putHistory(LocalStorageKeys.uploadHistory, hash.reference.toHex(), getAssetNameFromFiles(files))
|
putHistory(LocalStorageKeys.uploadHistory, hash.reference.toHex(), getAssetNameFromFiles(files))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user