Add selection download function

pull/37/head
Varun Patil 2022-09-08 16:51:48 -07:00
parent 13da56a2f6
commit 97a1dcddda
2 changed files with 54 additions and 0 deletions

View File

@ -72,6 +72,14 @@
{{ selection.size }} item(s) selected
</div>
<NcActions>
<NcActionButton
:aria-label="t('memories', 'Download selection')"
@click="downloadSelection"
class="icon-download">
{{ t('memories', 'Download') }}
</NcActionButton>
</NcActions>
<NcActions>
<NcActionButton
:aria-label="t('memories', 'Delete selection')"
@ -752,6 +760,17 @@ export default {
await this.deleteFromViewWithAnimation(delIds, updatedDays);
},
/** Download the selected files */
async downloadSelection() {
if (this.selection.size === 0) {
return;
}
// Get files to download
const fileInfos = await dav.getFiles([...this.selection].map(p => p.fileid));
await dav.downloadFiles(fileInfos.map(f => f.filename));
},
/**
* Delete elements from main view with some animation
* This function looks horribly slow, probably isn't that bad

View File

@ -1,4 +1,5 @@
import { getCurrentUser } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'
import { genFileInfo } from './FileUtils'
import client from './DavClient';
@ -135,3 +136,37 @@ export async function deleteFile(path) {
const prefixPath = `/files/${getCurrentUser().uid}`;
return await client.deleteFile(`${prefixPath}${path}`);
}
/**
* Download a file
*
* @param {string[]} fileNames - The file's names
*/
export async function downloadFiles(fileNames) {
const randomToken = Math.random().toString(36).substring(2)
const params = new URLSearchParams()
params.append('files', JSON.stringify(fileNames))
params.append('downloadStartSecret', randomToken)
const downloadURL = generateUrl(`/apps/files/ajax/download.php?${params}`)
window.location = `${downloadURL}downloadStartSecret=${randomToken}`
return new Promise((resolve) => {
const waitForCookieInterval = setInterval(
() => {
const cookieIsSet = document.cookie
.split(';')
.map(cookie => cookie.split('='))
.findIndex(([cookieName, cookieValue]) => cookieName === 'ocDownloadStarted' && cookieValue === randomToken)
if (cookieIsSet) {
clearInterval(waitForCookieInterval)
resolve(true)
}
},
50
)
})
}