Merge pull request #13465 from gitstart/fix/get-url-in-safari

fix: Get Url in Storage doesn't work in Safari
This commit is contained in:
Terry Sutton authored and GitHub committed 2023-04-17 21:48:47 -02:30
commit e781edcbde
2 files changed
+26 -8

No files matched your search

+18 -2
View File
@@ -153,10 +153,26 @@ export const snakeToCamel = (str: string) =>
group.toUpperCase().replace('-', '').replace('_', '')
)
export const copyToClipboard = (str: string, callback = () => {}) => {
/**
* Copy text content (string or Promise<string>) into Clipboard.
* Safari doesn't support write text into clipboard async, so if you need to load
* text content async before coping, please use Promise<string> for the 1st arg.
*/
export const copyToClipboard = (str: string | Promise<string>, callback = () => {}) => {
const focused = window.document.hasFocus()
if (focused) {
window.navigator?.clipboard?.writeText(str).then(callback)
if (window.ClipboardItem) {
const text = new ClipboardItem({
'text/plain': Promise.resolve(str).then((text) => new Blob([text], { type: 'text/plain' })),
})
window.navigator?.clipboard?.write([text]).then(callback)
return
}
Promise.resolve(str)
.then((text) => window.navigator?.clipboard?.writeText(text))
.then(callback)
} else {
console.warn('Unable to copy to clipboard')
}
@@ -407,13 +407,15 @@ class StorageExplorerStore {
})
} else {
// Need to generate signed URL, and might as well save it to cache as well
const signedUrl = await this.fetchFilePreview(file.name, expiresIn)
try {
let formattedUrl = new URL(signedUrl)
const signedUrlAsync = this.fetchFilePreview(file.name, expiresIn).then((signedUrl) => {
const formattedUrl = new URL(signedUrl)
formattedUrl.searchParams.set('t', new Date().toISOString())
copyToClipboard(formattedUrl.toString(), () => {
return formattedUrl.toString()
})
try {
copyToClipboard(signedUrlAsync, () => {
this.ui.setNotification({
category: 'success',
message: `Copied URL for ${file.name} to clipboard.`,
@@ -422,7 +424,7 @@ class StorageExplorerStore {
})
const fileCache = {
id: file.id,
url: formattedUrl.toString(),
url: await signedUrlAsync,
expiresIn: DEFAULT_EXPIRY,
fetchedAt: Date.now(),
}