Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(frontend:files): cancel uploads from tasks sidebar

johavenb01dc9047b48
diff --git a/frontend/src/app/applications/files/components/sidebar/files-tasks.component.html b/frontend/src/app/applications/files/components/sidebar/files-tasks.component.html
index 8af5262c..8a0409aa 100644
--- a/frontend/src/app/applications/files/components/sidebar/files-tasks.component.html
+++ b/frontend/src/app/applications/files/components/sidebar/files-tasks.component.html
@@ -27,30 +27,45 @@
<!-- Inner menu: contains the tasks -->
<div appAutoResize>
@for (task of tasks; track task.id) {
- <div class="task-list">
- <div (click)="goToFile(task)" [tooltip]="task.result | translate:locale.language" class="text-truncate" placement="bottom">
- <div>
- @if (canCancel(task)) {
- <button (click)="cancelTask($event, task)" class="btn btn-sm btn-default float-end">
- <fa-icon [icon]="icons.faStop"></fa-icon>
- </button>
- }
- <fa-icon [class.text-danger]="isTaskError(task)" [class.text-muted]="isTaskCancelled(task)" [icon]="iconsStatus[task.status]"
- [animation]="task.status === 0 ? 'spin-pulse' : null"></fa-icon>
- <b>
- <fa-icon [icon]="iconsOperation[task.type]" class="me-1"></fa-icon>
- </b>
- <span>{{ task.name }}</span>
+ @let pending = isTaskPending(task);
+ @let error = isTaskError(task);
+ @let cancelled = isTaskCancelled(task);
+ @let openable = canOpenTask(task);
+ @let cancellable = canCancel(task);
+ <div class="task-list"
+ [class.task-list--cancelled]="cancelled"
+ [class.task-list--error]="error"
+ [class.task-list--interactive]="openable"
+ [class.task-list--pending]="pending"
+ [tooltip]="task.result ? (task.result | translate:locale.language) : ''"
+ (click)="openTask(task)"
+ placement="bottom">
+ <div class="task-list__content">
+ <div class="task-list__header">
+ <fa-icon class="task-list__status"
+ [class.text-danger]="error"
+ [class.text-muted]="cancelled"
+ [icon]="iconsStatus[task.status]"
+ [animation]="pending ? 'spin-pulse' : null"></fa-icon>
+ <span class="task-list__operation">
+ <fa-icon [icon]="iconsOperation[task.type]"></fa-icon>
+ </span>
+ <span class="task-list__name">{{ task.name }}</span>
</div>
- @if (task.status >= 2) {
- <div [class.text-danger]="isTaskError(task)" [class.text-muted]="isTaskCancelled(task)" class="ms-2">{{ task.result | translate:locale.language }}</div>
+ @if (task.status >= 2 && !cancelled) {
+ <div [class.text-danger]="error" class="task-list__result">
+ {{ task.result | translate:locale.language }}
+ </div>
}
- <div class="mt-1 fs-sm">
+ </div>
+ <div class="task-list__progress-row">
+ <div class="task-list__progress">
<progressbar [max]="100"
[value]="task.status > 0 ? 100 : task.props.progress || 100"
- [type]="task.status < 1 ? 'warning' : isTaskError(task) ? 'danger' : null"
+ [type]="pending ? 'warning' : error ? 'danger' : null"
+ [class.custom-progress-bar--cancelled]="cancelled"
class="custom-progress-bar position-relative">
- <span class="d-flex align-items-center justify-content-center position-absolute mx-1 w-100">
+ <span class="task-list__progress-label d-flex align-items-center justify-content-center position-absolute w-100">
@if (task.props.size) {
<span>{{ task.props.size | toBytes:2 }}&nbsp;•&nbsp;</span>
}
@@ -67,6 +82,15 @@
</span>
</progressbar>
</div>
+ @if (cancellable) {
+ <button type="button"
+ (click)="cancelTask($event, task)"
+ [tooltip]="'Stop' | translate:locale.language"
+ class="btn btn-sm btn-default task-list__cancel"
+ placement="top">
+ <fa-icon [icon]="icons.faStop"></fa-icon>
+ </button>
+ }
</div>
</div>
}
diff --git a/frontend/src/app/applications/files/components/sidebar/files-tasks.component.ts b/frontend/src/app/applications/files/components/sidebar/files-tasks.component.ts
index b0c45cfc..962cf95f 100644
--- a/frontend/src/app/applications/files/components/sidebar/files-tasks.component.ts
+++ b/frontend/src/app/applications/files/components/sidebar/files-tasks.component.ts
@@ -93,6 +93,14 @@ export class FilesTasksComponent implements OnDestroy {
return this.filesTasksService.canCancel(task)
}
+ canOpenTask(task: FileTask): boolean {
+ return task.status === FileTaskStatus.SUCCESS
+ }
+
+ isTaskPending(task: FileTask): boolean {
+ return task.status === FileTaskStatus.PENDING
+ }
+
isTaskError(task: FileTask): boolean {
return task.status === FileTaskStatus.ERROR
}
@@ -101,20 +109,19 @@ export class FilesTasksComponent implements OnDestroy {
return task.status === FileTaskStatus.CANCELLED
}
- goToFile(task: FileTask) {
- if (task.status === 1) {
- if (task.type === FILE_OPERATION.COMPRESS && task.props.compressInDirectory === false) {
- this.filesService.downloadTaskArchive(task.id)
+ openTask(task: FileTask) {
+ if (!this.canOpenTask(task)) return
+ if (task.type === FILE_OPERATION.COMPRESS && task.props.compressInDirectory === false) {
+ this.filesService.downloadTaskArchive(task.id)
+ return
+ } else if (task.type === FILE_OPERATION.DELETE) {
+ if (task.path.startsWith(SPACES_PATH.FILES)) {
+ task.path = task.path.replace(SPACES_PATH.FILES, SPACES_PATH.TRASH)
+ } else if (task.path.startsWith(SPACES_PATH.SHARES)) {
+ // cannot access to the space referenced by the share
return
- } else if (task.type === FILE_OPERATION.DELETE) {
- if (task.path.startsWith(SPACES_PATH.FILES)) {
- task.path = task.path.replace(SPACES_PATH.FILES, SPACES_PATH.TRASH)
- } else if (task.path.startsWith(SPACES_PATH.SHARES)) {
- // cannot access to the space referenced by the share
- return
- }
}
- this.router.navigate([`${SPACES_PATH.SPACES}/${task.path}`], { queryParams: { select: task.name } }).catch(console.error)
}
+ this.router.navigate([`${SPACES_PATH.SPACES}/${task.path}`], { queryParams: { select: task.name } }).catch(console.error)
}
}
diff --git a/frontend/src/app/applications/files/interfaces/file-upload.interface.ts b/frontend/src/app/applications/files/interfaces/file-upload.interface.ts
index 8285e5e8..16ee3901 100644
--- a/frontend/src/app/applications/files/interfaces/file-upload.interface.ts
+++ b/frontend/src/app/applications/files/interfaces/file-upload.interface.ts
@@ -1,3 +1,14 @@
+import type { FileTask } from '@sync-in-server/backend/src/applications/files/models/file-task'
+import type { Observable } from 'rxjs'
+
export interface FileUpload extends File {
relativePath?: string
}
+
+export interface FileUploadTaskRequest {
+ controller: AbortController
+ done: boolean
+ req: Observable<any>
+ started: boolean
+ task: FileTask
+}
diff --git a/frontend/src/app/applications/files/services/files-tasks.service.ts b/frontend/src/app/applications/files/services/files-tasks.service.ts
index f62c92b3..5ec9ae6f 100644
--- a/frontend/src/app/applications/files/services/files-tasks.service.ts
+++ b/frontend/src/app/applications/files/services/files-tasks.service.ts
@@ -54,6 +54,7 @@ export class FilesTasksService {
}
private currentUserId: number
private cancellingTasks = new Set<string>()
+ private uploadCancellationHandlers = new Map<string, () => void>()
private watcher: Subscription = null
private watch = timer(1000, 1000).pipe(tap(() => this.doWatch()))
@@ -103,6 +104,15 @@ export class FilesTasksService {
cancel(task: FileTask) {
if (!this.canCancel(task)) return
this.cancellingTasks.add(task.id)
+ if (task.type === FILE_OPERATION.UPLOAD) {
+ const cancelUpload = this.uploadCancellationHandlers.get(task.id)
+ if (cancelUpload) {
+ cancelUpload()
+ } else {
+ this.cancellingTasks.delete(task.id)
+ }
+ return
+ }
this.http.post<void>(`${API_FILES_TASKS_CANCEL}/${task.id}`, null).subscribe({
error: (e) => {
this.cancellingTasks.delete(task.id)
@@ -115,10 +125,22 @@ export class FilesTasksService {
return (
task.status === FileTaskStatus.PENDING &&
!this.cancellingTasks.has(task.id) &&
- (task.type === FILE_OPERATION.DOWNLOAD || task.type === FILE_OPERATION.COMPRESS || task.type === FILE_OPERATION.DECOMPRESS)
+ (task.type === FILE_OPERATION.DOWNLOAD ||
+ task.type === FILE_OPERATION.COMPRESS ||
+ task.type === FILE_OPERATION.DECOMPRESS ||
+ (task.type === FILE_OPERATION.UPLOAD && this.uploadCancellationHandlers.has(task.id)))
)
}
+ registerUploadCancellation(taskId: string, cancel: () => void) {
+ this.uploadCancellationHandlers.set(taskId, cancel)
+ this.store.filesActiveTasks.next([...this.store.filesActiveTasks.getValue()])
+ }
+
+ unregisterUploadCancellation(taskId: string) {
+ this.uploadCancellationHandlers.delete(taskId)
+ }
+
updateTask(task: FileTask) {
if (task.status === FileTaskStatus.PENDING) {
const currentTask = this.findTask(task.id, true)
diff --git a/frontend/src/app/applications/files/services/files-upload.service.ts b/frontend/src/app/applications/files/services/files-upload.service.ts
index 01be34b1..0092a1bf 100644
--- a/frontend/src/app/applications/files/services/files-upload.service.ts
+++ b/frontend/src/app/applications/files/services/files-upload.service.ts
@@ -1,11 +1,11 @@
import { HttpClient, HttpEventType, HttpUploadProgressEvent } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import { API_FILES_OPERATION_UPLOAD } from '@sync-in-server/backend/src/applications/files/constants/routes'
-import { FileTask, FileTaskStatus } from '@sync-in-server/backend/src/applications/files/models/file-task'
-import { lastValueFrom, Observable } from 'rxjs'
-import { filter, tap } from 'rxjs/operators'
+import type { FileTask } from '@sync-in-server/backend/src/applications/files/models/file-task'
+import { FileTaskStatus } from '@sync-in-server/backend/src/applications/files/models/file-task'
+import { Observable, Subscription } from 'rxjs'
import { supportUploadDirectory } from '../../../common/utils/functions'
-import { FileUpload } from '../interfaces/file-upload.interface'
+import type { FileUpload, FileUploadTaskRequest } from '../interfaces/file-upload.interface'
import { FileModel } from '../models/file.model'
import { FilesTasksService } from './files-tasks.service'
import { FilesService } from './files.service'
@@ -19,33 +19,46 @@ export class FilesUploadService {
async addFiles(files: FileUpload[], overwrite: boolean) {
const apiRoute = `${API_FILES_OPERATION_UPLOAD}/${this.filesService.currentRoute}`
- const taskReqs: [FileTask, Observable<any>][] = []
+ const taskReqs: FileUploadTaskRequest[] = []
for (const [key, data] of Object.entries(this.sortFiles(files))) {
const path = `${this.filesService.currentRoute}/${key}`.split('/').slice(0, -1).join('/')
const name = `${this.filesService.currentRoute}/${key}`.split('/').slice(-1)[0]
const task: FileTask = this.filesTasksService.createUploadTask(path, name, data.size)
- taskReqs.unshift([
- task,
- this.uploadFiles(`${apiRoute}/${key}`, data.form, overwrite).pipe(
- filter((ev: any) => ev.type === HttpEventType.UploadProgress),
- tap((ev: HttpUploadProgressEvent) => this.updateProgress(task, ev))
- )
- ])
+ const controller = new AbortController()
+ const taskReq = {
+ controller,
+ done: false,
+ req: this.uploadFiles(`${apiRoute}/${key}`, data.form, overwrite),
+ started: false,
+ task
+ }
+ this.filesTasksService.registerUploadCancellation(task.id, () => this.cancelUploadTask(taskReq))
+ taskReqs.unshift(taskReq)
}
- for (const [task, req] of taskReqs) {
+ for (const taskReq of taskReqs) {
+ const { controller, req, task } = taskReq
+ if (taskReq.done) continue
+ taskReq.started = true
try {
- await lastValueFrom(req)
+ await this.runUpload(task, req, controller.signal)
task.props.progress = 100
task.status = FileTaskStatus.SUCCESS
} catch (e: any) {
- task.status = FileTaskStatus.ERROR
- if (e.status === 0) {
- task.result = e.statusText
+ if (this.isUploadCancelled(e)) {
+ task.status = FileTaskStatus.CANCELLED
+ task.result = 'Cancelled'
} else {
- task.result = e.error.message
+ task.status = FileTaskStatus.ERROR
+ if (e.status === 0) {
+ task.result = e.statusText
+ } else {
+ task.result = e.error?.message || e.message
+ }
}
} finally {
+ taskReq.done = true
+ this.filesTasksService.unregisterUploadCancellation(task.id)
this.filesTasksService.updateTask(task)
}
}
@@ -80,6 +93,53 @@ export class FilesUploadService {
})
}
+ private runUpload(task: FileTask, req: Observable<any>, signal: AbortSignal): Promise<void> {
+ return new Promise((resolve, reject) => {
+ if (signal.aborted) {
+ reject(signal.reason)
+ return
+ }
+ let settled = false
+ const subscription = new Subscription()
+ const onAbort = () => {
+ subscription.unsubscribe()
+ settle(() => reject(signal.reason))
+ }
+ const settle = (done: () => void) => {
+ if (settled) return
+ settled = true
+ signal.removeEventListener('abort', onAbort)
+ done()
+ }
+ signal.addEventListener('abort', onAbort, { once: true })
+ subscription.add(
+ req.subscribe({
+ next: (ev: any) => {
+ if (ev.type === HttpEventType.UploadProgress) {
+ this.updateProgress(task, ev)
+ }
+ },
+ error: (e) => settle(() => reject(e)),
+ complete: () => settle(resolve)
+ })
+ )
+ })
+ }
+
+ private cancelUploadTask(taskReq: FileUploadTaskRequest) {
+ taskReq.controller.abort(new Error('Cancelled'))
+ if (taskReq.started || taskReq.done) return
+ taskReq.done = true
+ taskReq.task.status = FileTaskStatus.CANCELLED
+ taskReq.task.result = 'Cancelled'
+ this.filesTasksService.unregisterUploadCancellation(taskReq.task.id)
+ this.filesTasksService.updateTask(taskReq.task)
+ }
+
+ private isUploadCancelled(e: any): boolean {
+ return e?.message === 'Cancelled'
+ }
+
private updateProgress(task: FileTask, ev: HttpUploadProgressEvent) {
task.props.size = ev.loaded
task.props.progress = Math.round((100 * ev.loaded) / ev.total)
diff --git a/frontend/src/styles/components/_tasks.scss b/frontend/src/styles/components/_tasks.scss
index 32046b2c..6126a125 100755
--- a/frontend/src/styles/components/_tasks.scss
+++ b/frontend/src/styles/components/_tasks.scss
@@ -1,20 +1,109 @@
+$task-progress-height: 1.35rem;
+
.task-list {
- @include no-select-pointer-utility;
+ @include no-select-utility;
border-bottom: .5px solid transparent;
+ border-left: 3px solid transparent;
font-size: $font-size-sm;
+ padding: .3rem .4rem;
+ transition: background-color .16s ease, border-left-color .16s ease;
+
+ &--pending {
+ border-left-color: $warning;
+ }
+
+ &--error {
+ border-left-color: $danger;
+ }
+
+ &--cancelled {
+ border-left-color: $secondary;
+ }
+
+ &--interactive {
+ cursor: pointer;
+ }
+
+ &__content {
+ min-width: 0;
+ }
+
+ &__header {
+ align-items: center;
+ display: flex;
+ gap: .25rem;
+ min-width: 0;
+ }
+
+ &__status,
+ &__operation {
+ flex: 0 0 .95rem;
+ }
+
+ &__operation {
+ opacity: .85;
+ }
+
+ &__name,
+ &__result,
+ &__progress-label {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+
+ &__name {
+ flex: 1 1 auto;
+ min-width: 0;
+ }
- &:hover {
- background-color: transparent;
+ &__result {
+ line-height: 1.2;
+ margin: .2rem 0 0;
}
- > div {
- padding: 0.3rem;
- //max-height: 50px;
+ &__progress-row {
+ align-items: center;
+ display: flex;
+ gap: .35rem;
+ margin-top: .4rem;
+ min-width: 0;
+ }
+
+ &__progress {
+ flex: 1 1 auto;
+ min-width: 0;
+ }
+
+ &__progress-label {
+ inset: 0;
+ min-width: 0;
+ padding: 0 .5rem;
+ text-shadow: 0 1px 1px rgba($black, .32);
+ }
+
+ &__cancel {
+ align-items: center;
+ display: inline-flex;
+ flex: 0 0 auto;
+ height: $task-progress-height;
+ justify-content: center;
+ min-width: 1.9rem;
+ padding: 0;
}
}
.custom-progress-bar {
- background-color: black;
+ background-color: rgba($black, .82);
+ border-radius: .45rem;
+ display: block;
font-size: $font-size-xs;
+ height: $task-progress-height;
+ overflow: hidden;
+ &--cancelled {
+ .progress-bar {
+ background-color: mix($gray-dark, $gray, 72%) !important;
+ }
+ }
}
diff --git a/frontend/src/styles/components/_theme_dark.scss b/frontend/src/styles/components/_theme_dark.scss
index c646e8ce..12e7de3a 100755
--- a/frontend/src/styles/components/_theme_dark.scss
+++ b/frontend/src/styles/components/_theme_dark.scss
@@ -453,7 +453,7 @@
.task-list {
border-bottom-color: $sidebar-dark-icons-menu-border-color;
- &:hover {
+ &.task-list--interactive:hover {
background-color: $sidebar-dark-apps-menu-title-bg;
}
}
diff --git a/frontend/src/styles/components/_theme_light.scss b/frontend/src/styles/components/_theme_light.scss
index b742e7ed..8ac223c0 100755
--- a/frontend/src/styles/components/_theme_light.scss
+++ b/frontend/src/styles/components/_theme_light.scss
@@ -478,7 +478,7 @@
.task-list {
border-bottom-color: $gray-mid;
- &:hover {
+ &.task-list--interactive:hover {
background-color: $table-bg-hover;
}
}