Skip to content

Commit

Permalink
Merge pull request #501 from fdm-monster/chore/outdated-file-model
Browse files Browse the repository at this point in the history
fix: bring FileDto up-to-date with backend cache model (outdated OctoPrint model removed)
  • Loading branch information
davidzwa authored Nov 4, 2024
2 parents 8edb3ca + f35f6b3 commit ab82e99
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 221 deletions.
30 changes: 9 additions & 21 deletions src/backend/printer-file.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { BaseService } from '@/backend/base.service'
import { ServerApi } from '@/backend/server.api'
import { FileUploadCommands } from '@/models/printers/file-upload-commands.model'
import {
ClearedFilesResult,
FileDto
} from '@/models/printers/printer-file.model'
import { PrinterDto } from '@/models/printers/printer.model'
import { useSnackbar } from '@/shared/snackbar.composable'
import { IdType } from '@/utils/id.type'
import { downloadFileByBlob } from '@/utils/download-file.util'

export class PrinterFileService extends BaseService {
static async getFiles(printerId: IdType, recursive = false) {
const path = `${ServerApi.printerFilesRoute}/${printerId}/?recursive=${recursive}`
static async getFiles(printerId: IdType) {
const path = `${ServerApi.printerFilesRoute}/${printerId}`

return (await this.get(path)) as FileDto[]
}
Expand All @@ -35,23 +35,10 @@ export class PrinterFileService extends BaseService {
return await this.post(path, { filePath, print })
}

static async uploadFile(
printer: PrinterDto,
file: File,
commands: FileUploadCommands = {
select: true,
print: true
}
) {
static async uploadFile(printer: PrinterDto, file: File) {
const path = ServerApi.printerFilesUploadRoute(printer.id)

const formData = new FormData()
if (commands.select) {
formData.append('select', 'true')
}
if (commands.print) {
formData.append('print', 'true')
}
formData.append('files[0]', file)

return this.postUpload(path, formData, {
Expand Down Expand Up @@ -80,12 +67,13 @@ export class PrinterFileService extends BaseService {
}

static async deleteFileOrFolder(printerId: IdType, path: string) {
const urlPath = `${ServerApi.printerFilesRoute}/${printerId}/?path=${path}`

const urlPath = `${ServerApi.printerFilesRoute}/${printerId}?path=${path}`
return this.delete(urlPath)
}

static downloadFile(file: FileDto) {
window.location.href = file.refs.download
static async downloadFile(printerId: IdType, path: string) {
const urlPath = `${ServerApi.printerFilesRoute}/${printerId}/download/${path}`
const arrayBuffer = await this.getDownload(urlPath)
downloadFileByBlob(arrayBuffer.data, path)
}
}
17 changes: 1 addition & 16 deletions src/components/Generic/Actions/RefreshFilesAction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import { usePrinterStore } from '@/store/printer.store'
import { defineComponent, PropType } from 'vue'
import { PrinterDto } from '@/models/printers/printer.model'
interface Data {
property: number
}
export default defineComponent({
name: 'RefreshFilesAction',
components: {},
Expand All @@ -28,22 +24,11 @@ export default defineComponent({
printersStore: usePrinterStore()
}
},
data: (): Data => ({
property: 0
}),
computed: {},
watch: {},
async created() {},
async mounted() {},
methods: {
async getFiles() {
if (!this.printer) return
await this.printersStore.loadPrinterFiles(this.printer.id, false)
await this.printersStore.loadPrinterFiles(this.printer.id)
}
}
})
Expand Down
113 changes: 87 additions & 26 deletions src/components/Generic/FileExplorerSideNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</v-avatar>
</v-btn>
</template>
<span> Visit the OctoPrint associated to this printer </span>
<span> Visit the {{ serviceName }} associated to this printer </span>
</v-tooltip>
</template>

Expand Down Expand Up @@ -96,10 +96,10 @@
color="primary"
>
<span v-if="!isEnabled">
Disabled OctoPrint, enable it first to get live updates
Disabled {{ serviceName }}, enable it first to get live updates
</span>
<span v-else>
This OctoPrint seems unreachable... Will keep trying for you
This {{ serviceName }} seems unreachable... Will keep trying for you
<v-icon>hourglass_top</v-icon>
</span>
</v-alert>
Expand All @@ -109,13 +109,13 @@
"
color="secondary"
>
This OctoPrint was disabled without reason.
This {{ serviceName }} was disabled without reason.
</v-alert>
<v-alert
v-if="storedSideNavPrinter?.disabledReason"
color="black"
>
This OctoPrint was disabled for maintenance: <br />
This {{ serviceName }} was disabled for maintenance: <br />
<small> &nbsp;&nbsp;{{ storedSideNavPrinter?.disabledReason }} </small>
</v-alert>

Expand All @@ -140,13 +140,42 @@
class="ml-3 mr-6 ma-5"
:size="iconSize"
>
<v-img src="/img/octoprint-tentacle.svg" />
<v-img
v-if="isOctoPrint"
src="/img/octoprint-tentacle.svg"
/>
<span v-else>MO</span>
</v-avatar>
</template>
<span> Open OctoPrint </span>
<span> Open {{ serviceName }} </span>
</v-list-item>
</template>
<span> Visit the OctoPrint associated to this printer </span>
<span> Visit the {{ serviceName }} associated to this printer </span>
</v-tooltip>

<v-tooltip
location="left"
v-if="isMoonraker"
>
<template #activator="{ props }">
<v-list-item
class="extra-dense-list-item"
link
v-bind="props"
@click.prevent.stop="openPrinterMainsail()"
>
<template #prepend>
<v-avatar
class="ml-3 mr-6 ma-5"
:size="iconSize"
>
<span>MA</span>
</v-avatar>
</template>
<span> Open Mainsail </span>
</v-list-item>
</template>
<span> Visit the Mainsail for this printer </span>
</v-tooltip>

<v-tooltip location="left">
Expand Down Expand Up @@ -378,7 +407,7 @@
<template #activator="{ props }">
<v-btn
v-bind="props"
@click="clickDownloadFile(file)"
@click="clickDownloadFile(file.path)"
>
<v-icon> download </v-icon>
</v-btn>
Expand Down Expand Up @@ -409,11 +438,11 @@
:class="{ 'current-file-print': isFileBeingPrinted(file) }"
v-bind="props"
>
{{ file.name }}
{{ file.path }}
</span>
</template>
<span>
File: {{ file.name }} <br />
File: {{ file.path }} <br />
Size: {{ formatBytes(file.size) }} <br />
<strong>
{{ isFileBeingPrinted(file) ? 'Printing' : 'Unused' }}
Expand Down Expand Up @@ -454,11 +483,17 @@ import { usePrinterStateStore } from '@/store/printer-state.store'
import { interpretStates } from '@/shared/printer-state.constants'
import { useSettingsStore } from '@/store/settings.store'
import { useFeatureStore } from '@/store/features.store'
import {
isMoonrakerType,
isOctoPrintType,
getServiceName
} from '@/utils/printer-type.utils'
const printersStore = usePrinterStore()
const printerStateStore = usePrinterStateStore()
const dialogsStore = useDialogsStore()
const featureStore = useFeatureStore()
const iconSize = ref(36)
const fileSearch = ref<string | undefined>(undefined)
const shownFileCache = ref<FileDto[] | undefined>(undefined)
Expand All @@ -469,6 +504,19 @@ const printerId = computed(() => storedSideNavPrinter.value?.id)
const isOnline = computed(() =>
printerId.value ? printerStateStore.isApiResponding(printerId.value) : false
)
const isOctoPrint = computed(() => {
return isOctoPrintType(storedSideNavPrinter.value?.printerType)
})
const isMoonraker = computed(() => {
return isMoonrakerType(storedSideNavPrinter.value?.printerType)
})
const serviceName = computed(() =>
getServiceName(storedSideNavPrinter.value?.printerType)
)
const isOperational = computed(() =>
printerId.value
? printerStateStore.isPrinterOperational(printerId.value)
Expand All @@ -490,7 +538,7 @@ const filesListed = computed(() => {
return (
shownFileCache.value.filter((f) =>
fileSearch.value?.length
? `${f.name}${f.path}`.toLowerCase().includes(fileSearch.value)
? `${f.path}`.toLowerCase().includes(fileSearch.value)
: true
) || []
)
Expand Down Expand Up @@ -550,23 +598,25 @@ const refreshFiles = async () => {
loading.value = true
const currentPrinterId = storedSideNavPrinter.value?.id
if (!currentPrinterId) return
if (printerStateStore.isApiResponding(currentPrinterId)) {
shownFileCache.value = await printersStore.loadPrinterFiles(
currentPrinterId,
false
)
} else {
shownFileCache.value = await PrinterFileService.getFileCache(
currentPrinterId
)
try {
if (printerStateStore.isApiResponding(currentPrinterId)) {
shownFileCache.value = await printersStore.loadPrinterFiles(
currentPrinterId
)
} else {
shownFileCache.value = await PrinterFileService.getFileCache(
currentPrinterId
)
}
} finally {
loading.value = false
}
loading.value = false
}
const deleteFile = async (file: FileDto) => {
if (!printerId.value) return
await printersStore.deletePrinterFile(printerId.value, file.path)
}
// ... (continue with other methods)
watch(storedSideNavPrinter, async (viewedPrinter, oldVal) => {
drawerOpened.value = !!viewedPrinter
const currentPrinterId = viewedPrinter?.id
Expand All @@ -577,6 +627,7 @@ watch(storedSideNavPrinter, async (viewedPrinter, oldVal) => {
await refreshFiles()
}
})
watch(drawerOpened, (newVal) => {
if (!newVal) {
printersStore.setSideNavPrinter(undefined)
Expand All @@ -594,7 +645,7 @@ function isFileBeingPrinted(file: FileDto) {
}
const jobFilePath =
printerStateStore.printingFilePathsByPrinterId[printerId.value]
return jobFilePath === file.name
return jobFilePath === file.path
}
function avatarInitials() {
Expand All @@ -610,6 +661,15 @@ function openPrinterURL() {
closeDrawer()
}
function openPrinterMainsail() {
if (!storedSideNavPrinter.value) return
const url = new URL(storedSideNavPrinter.value.printerURL)
url.port = '8080'
PrintersService.openPrinterURL(url.toString())
closeDrawer()
}
async function togglePrinterConnection() {
if (!printerId.value) return
if (printerStateStore.isPrinterOperational(printerId.value)) {
Expand Down Expand Up @@ -691,8 +751,9 @@ async function clickPrintFile(file: FileDto) {
})
}
function clickDownloadFile(file: FileDto) {
PrinterFileService.downloadFile(file)
function clickDownloadFile(path: string) {
if (!printerId.value) return
PrinterFileService.downloadFile(printerId.value, path)
}
function closeDrawer() {
Expand Down
12 changes: 2 additions & 10 deletions src/directives/file-upload.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const bindDropConditionally = (
// Convert the file and bound printer to a file upload
convertedUploads = convertPrinterMultiFileToQueue(
firstPrinter,
clonedFiles,
printedFilename
clonedFiles
)
} else {
if (clonedFiles.length > 1) {
Expand All @@ -56,14 +55,7 @@ const bindDropConditionally = (
clonedFiles.length
)
const clonedFile = clonedFiles[0]
convertedUploads = convertMultiPrinterFileToQueue(
printers,
clonedFile,
{
select: true,
print: true
}
)
convertedUploads = convertMultiPrinterFileToQueue(printers, clonedFile)
}

uploadsStore.queueUploads(convertedUploads)
Expand Down
Loading

0 comments on commit ab82e99

Please sign in to comment.