-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of archiver v2
- Loading branch information
Showing
9 changed files
with
186 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
packages/web-app-files/src/helpers/download/downloadAsArchive.ts
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,31 @@ | ||
import { archiverService, clientService } from '../../services' | ||
import { AxiosResponse } from 'axios' | ||
import { major } from 'semver' | ||
|
||
interface TriggerDownloadAsArchiveOptions { | ||
folderPath: string | ||
fileNames?: string[] | ||
token: string // TODO: solve download from a) public link b) public link with password | ||
} | ||
|
||
export const triggerDownloadAsArchive = async ( | ||
options: TriggerDownloadAsArchiveOptions | ||
): Promise<AxiosResponse> => { | ||
if (!isDownloadAsArchiveAvailable()) { | ||
return | ||
} | ||
const majorVersion = major(archiverService.capability.version) | ||
switch (majorVersion) { | ||
case 2: { | ||
const queryParams = ['dir=' + options.folderPath, ...options.fileNames.map(f => 'file=' + f)] | ||
const url = archiverService.url + '?' + queryParams.join('&') | ||
return await clientService.httpAuthenticated(options.token).get(url) | ||
} | ||
default: | ||
// do nothing for unknown versions | ||
} | ||
} | ||
|
||
export const isDownloadAsArchiveAvailable = (): boolean => { | ||
return archiverService.available | ||
} |
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
51 changes: 51 additions & 0 deletions
51
packages/web-app-files/src/mixins/actions/downloadFolder.js
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,51 @@ | ||
import { isFavoritesRoute, isPersonalRoute, isPublicFilesRoute } from '../../helpers/route' | ||
import { | ||
isDownloadAsArchiveAvailable, | ||
triggerDownloadAsArchive | ||
} from '../../helpers/download/downloadAsArchive' | ||
import { mapGetters } from 'vuex' | ||
|
||
export default { | ||
computed: { | ||
...mapGetters(['getToken']), | ||
$_downloadFolder_items() { | ||
return [ | ||
{ | ||
icon: 'archive', | ||
handler: this.$_downloadFolder_trigger, | ||
label: () => { | ||
return this.$gettext('Download folder') | ||
}, | ||
isEnabled: ({ resource }) => { | ||
if ( | ||
!isPersonalRoute(this.$route) && | ||
!isPublicFilesRoute(this.$route) && | ||
!isFavoritesRoute(this.$route) | ||
) { | ||
return false | ||
} | ||
if (!isDownloadAsArchiveAvailable()) { | ||
return false | ||
} | ||
if (!resource.isFolder) { | ||
return false | ||
} | ||
|
||
return resource.canDownload() | ||
}, | ||
canBeDefault: true, | ||
componentType: 'oc-button', | ||
class: 'oc-files-actions-download-archive-trigger' | ||
} | ||
] | ||
} | ||
}, | ||
methods: { | ||
async $_downloadFolder_trigger(resource) { | ||
await triggerDownloadAsArchive({ | ||
folderPath: resource.path, | ||
token: this.getToken | ||
}) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { rcompare } from 'semver' | ||
import { RuntimeError } from 'web-runtime/src/container/error' | ||
|
||
/** | ||
* Archiver struct within the capabilities as defined in reva | ||
* TODO: add github link | ||
*/ | ||
interface ArchiverCapability { | ||
enabled: boolean | ||
version: string // version is just a major version, e.g. `v2` | ||
formats: string[] | ||
// eslint-disable-next-line camelcase | ||
archiver_url: string | ||
} | ||
|
||
class ArchiverService { | ||
serverUrl: string | ||
capability?: ArchiverCapability | ||
|
||
public initialize(serverUrl: string, archiverCapabilities: ArchiverCapability[] = []): void { | ||
this.serverUrl = serverUrl | ||
const archivers = archiverCapabilities | ||
.filter(a => a.enabled) | ||
.sort((a1, a2) => rcompare(a1.version, a2.version)) | ||
this.capability = archivers.length ? archivers[0] : null | ||
} | ||
|
||
public get available() { | ||
return this.capability !== null | ||
} | ||
|
||
public get url() { | ||
if (!this.available) { | ||
throw new RuntimeError('no enabled archiver found') | ||
} | ||
return [ | ||
this.serverUrl.replace(/\/+$/, ''), | ||
this.capability.archiver_url.replace(/^\/+/, '') | ||
].join('/') | ||
} | ||
} | ||
|
||
export const archiverService = new ArchiverService() |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './archiver' | ||
export * from './cache' | ||
export * from './client' | ||
export { default as Registry } from './registry' |