Skip to content

Commit

Permalink
feat: adds support to create ZIP archives (#1007)
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas authored Jan 12, 2023
1 parent 2f1a59a commit 9d8d36b
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 12 deletions.
29 changes: 29 additions & 0 deletions src/api/socketActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,35 @@ export const SocketActions = {
)
},

async serverFilesCopy (source: string, dest: string) {
const wait = Waits.onFileSystem
baseEmit(
'server.files.copy', {
dispatch: 'void',
wait,
params: {
source,
dest
}
}
)
},

async serverFilesZip (dest: string, items: string[], store_only?: boolean) {
const wait = Waits.onFileSystem
baseEmit(
'server.files.zip', {
dispatch: 'void',
wait,
params: {
dest,
items,
store_only
}
}
)
},

/**
* Create a directory.
* Root should be included in the path.
Expand Down
13 changes: 13 additions & 0 deletions src/components/widgets/filesystem/FileSystem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
v-if="selected.length > 0"
:path="visiblePath"
@remove="handleRemove(selected)"
@create-zip="handleCreateZip(selected)"
/>

<file-system-browser
Expand Down Expand Up @@ -69,6 +70,7 @@
@preview-gcode="handlePreviewGcode"
@view-thumbnail="handleViewThumbnail"
@enqueue="handleEnqueue"
@create-zip="handleCreateZip"
/>

<file-editor-dialog
Expand Down Expand Up @@ -882,6 +884,17 @@ export default class FileSystem extends Mixins(StateMixin, FilesMixin, ServicesM
SocketActions.serverJobQueuePostJob([filepath])
}
handleCreateZip (file: FileBrowserEntry | FileBrowserEntry[]) {
const dest = Array.isArray(file)
? `${this.currentPath}/${Date.now()}.zip`
: `${this.currentPath}/${file.name}_${Date.now()}.zip`
const items = (Array.isArray(file) ? file : [file])
.map(item => `${this.currentPath}/${item.name}`)
SocketActions.serverFilesZip(dest, items)
}
/**
* ===========================================================================
* Drag handling.
Expand Down
16 changes: 15 additions & 1 deletion src/components/widgets/filesystem/FileSystemBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
:small="dense"
:color="(item.type === 'file') ? 'grey' : 'primary'"
>
{{ (item.type === 'file' ? '$file' : item.name === '..' ? '$folderUp' : '$folder') }}
{{ getItemIcon(item) }}
</v-icon>
<img
v-else
Expand Down Expand Up @@ -383,6 +383,20 @@ export default class FileSystemBrowser extends Mixins(FilesMixin) {
}
}
getItemIcon (item: FileBrowserEntry) {
if (item.type === 'file') {
if (item.extension === 'zip') {
return '$fileZip'
} else {
return '$file'
}
} else if (item.name === '..') {
return '$folderUp'
} else {
return '$folder'
}
}
// Determines if a row is currently in a draggable state or not.
draggable (item: FileBrowserEntry | AppFileWithMeta) {
return (
Expand Down
18 changes: 18 additions & 0 deletions src/components/widgets/filesystem/FileSystemBulkActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@

<v-spacer />

<v-tooltip bottom>
<template #activator="{ on, attrs }">
<v-btn
v-bind="attrs"
fab
small
text
v-on="on"
@click="$emit('create-zip')"
>
<v-icon>
$fileZipAdd
</v-icon>
</v-btn>
</template>
<span>{{ $t('app.general.btn.create_zip_archive') }}</span>
</v-tooltip>

<v-tooltip bottom>
<template #activator="{ on, attrs }">
<v-btn
Expand Down
34 changes: 26 additions & 8 deletions src/components/widgets/filesystem/FileSystemContextMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,34 @@
<v-list-item-title>{{ $t('app.general.btn.view') }}</v-list-item-title>
</v-list-item>
<v-list-item
v-if="file.type !== 'directory'"
v-if="canPreviewGcode"
link
@click="$emit('download', file)"
@click="$emit('preview-gcode', file)"
>
<v-list-item-icon>
<v-icon>$download</v-icon>
<v-icon>$magnify</v-icon>
</v-list-item-icon>
<v-list-item-title>{{ $t('app.general.btn.download') }}</v-list-item-title>
<v-list-item-title>{{ $t('app.general.btn.preview_gcode') }}</v-list-item-title>
</v-list-item>
<v-list-item
v-if="file.type !== 'directory' && canPreviewGcode"
v-if="canCreateZip"
link
@click="$emit('preview-gcode', file)"
@click="$emit('create-zip', file)"
>
<v-list-item-icon>
<v-icon>$magnify</v-icon>
<v-icon>$fileZipAdd</v-icon>
</v-list-item-icon>
<v-list-item-title>{{ $t('app.general.btn.preview_gcode') }}</v-list-item-title>
<v-list-item-title>{{ $t('app.general.btn.create_zip_archive') }}</v-list-item-title>
</v-list-item>
<v-list-item
v-if="file.type !== 'directory'"
link
@click="$emit('download', file)"
>
<v-list-item-icon>
<v-icon>$download</v-icon>
</v-list-item-icon>
<v-list-item-title>{{ $t('app.general.btn.download') }}</v-list-item-title>
</v-list-item>
<v-list-item
v-if="!rootProperties.readonly"
Expand Down Expand Up @@ -196,6 +206,14 @@ export default class FileSystemContextMenu extends Mixins(StateMixin, FilesMixin
)
}
get canCreateZip () {
return (
(this.file.type !== 'file' || this.file.extension !== 'zip') &&
!this.rootProperties.readonly &&
this.$store.getters['server/getIsMinApiVersion'](1, 1)
)
}
get supportsJobQueue (): boolean {
return this.$store.getters['server/componentSupport']('job_queue')
}
Expand Down
4 changes: 4 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ import {
mdiLayers,
mdiLayersPlus,
mdiFolderOpen,
mdiArchive,
mdiArchivePlus,
mdiTrayFull,
mdiTrayPlus
} from '@mdi/js'
Expand Down Expand Up @@ -347,6 +349,8 @@ export const Icons = Object.freeze({
extrusions: mdiPrinter3dNozzle,
retractions: mdiSwapVertical,
folderOpen: mdiFolderOpen,
fileZip: mdiArchive,
fileZipAdd: mdiArchivePlus,
jobQueue: mdiTrayFull,
enqueueJob: mdiTrayPlus
})
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ app:
clear_profile: Clear Profile
close: Close
config_reference: Config Reference
create_zip_archive: Create ZIP Archive
delete: Delete
download: Download
edit: Edit
Expand Down
5 changes: 5 additions & 0 deletions src/store/server/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GetterTree } from 'vuex'
import { ServerInfo, ServerConfig, ServerState, SystemInfo, ServerSystemStat, ServiceInfo, ServiceState } from './types'
import { RootState } from '../types'
import { Globals } from '@/globals'
import numberArrayCompare from '@/util/number-array-compare'

export const getters: GetterTree<ServerState, RootState> = {
/**
Expand All @@ -11,6 +12,10 @@ export const getters: GetterTree<ServerState, RootState> = {
return state.info
},

getIsMinApiVersion: (state) => (...version: number[]) => {
return numberArrayCompare(state.info.api_version ?? [], version) <= 0
},

/**
* Gets the current system info
*/
Expand Down
2 changes: 2 additions & 0 deletions src/store/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface ServerInfo {
components: string[];
registered_directories: string[];
warnings: string[];
api_version?: number[]
api_version_string?: string
}

export interface SystemInfo {
Expand Down
6 changes: 3 additions & 3 deletions src/store/version/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export const getters: GetterTree<VersionState, RootState> = {
*/
getCommitHistory: (state) => (component: string) => {
// This is only relevant for certain types.
if (state.version_info[component] && isOfType<HashVersion>(state.version_info[component], 'git_messages')) {
const c = state.version_info[component] as HashVersion
const result = [...c.commits_behind]
const componentVersionInfo = state.version_info[component]
if (state.version_info[component] && isOfType<HashVersion>(componentVersionInfo, 'git_messages')) {
const result = [...componentVersionInfo.commits_behind]
.reduce((groups, commitItem) => {
const dateAndTime = new Date(+commitItem.date * 1000)
const dateOnly = +(new Date(dateAndTime.getFullYear(), dateAndTime.getMonth(), dateAndTime.getDate()))
Expand Down
18 changes: 18 additions & 0 deletions src/util/number-array-compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const numberArrayCompare = (versionA: number[], versionB: number[]) => {
const count = Math.max(versionA.length, versionB.length)

for (let index = 0; index < count; index++) {
const a = versionA[index] ?? 0
const b = versionB[index] ?? 0

if (a > b) {
return -1
} else if (a < b) {
return 1
}
}

return 0
}

export default numberArrayCompare
1 change: 1 addition & 0 deletions src/views/Configure.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
:roots="['config']"
:max-height="816"
name="configure"
bulk-actions
/>
</collapsable-card>
</v-col>
Expand Down

0 comments on commit 9d8d36b

Please sign in to comment.