Skip to content

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

feat(files): add ZIP archive creation with optional compression

johaven7c94d6a235d6
diff --git a/backend/src/applications/files/constants/compress.ts b/backend/src/applications/files/constants/compress.ts
index c2cc0814..3c99f142 100644
--- a/backend/src/applications/files/constants/compress.ts
+++ b/backend/src/applications/files/constants/compress.ts
@@ -1,2 +1,3 @@
export const TAR_EXTENSION = 'tar'
export const TAR_GZ_EXTENSION = 'tgz'
+export const ZIP_EXTENSION = 'zip'
diff --git a/backend/src/applications/files/dto/file-operations.dto.ts b/backend/src/applications/files/dto/file-operations.dto.ts
index 91ba66fb..23c43e42 100644
--- a/backend/src/applications/files/dto/file-operations.dto.ts
+++ b/backend/src/applications/files/dto/file-operations.dto.ts
@@ -2,7 +2,7 @@ import { Transform } from 'class-transformer'
import { ArrayMinSize, IsArray, IsBoolean, IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString, IsUrl } from 'class-validator'
import { RejectIfMatch } from '../../../common/decorators'
import { regExpInvalidFileName } from '../../../common/shared'
-import { TAR_EXTENSION, TAR_GZ_EXTENSION } from '../constants/compress'
+import { TAR_EXTENSION, ZIP_EXTENSION } from '../constants/compress'
export class CopyMoveFileDto {
@IsNotEmpty()
@@ -51,6 +51,10 @@ export class CompressFileDto {
@IsBoolean()
compressInDirectory: boolean
+ @IsDefined()
+ @IsBoolean()
+ compression: boolean
+
@IsNotEmpty({ each: true })
@IsArray()
@ArrayMinSize(1)
@@ -58,8 +62,8 @@ export class CompressFileDto {
@IsNotEmpty()
@IsString()
- @IsIn([TAR_EXTENSION, TAR_GZ_EXTENSION])
- extension: typeof TAR_EXTENSION | typeof TAR_GZ_EXTENSION
+ @IsIn([TAR_EXTENSION, ZIP_EXTENSION])
+ extension: typeof TAR_EXTENSION | typeof ZIP_EXTENSION
}
export class SearchFilesDto {
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.ts
index 7f690045..13b42a6f 100644
--- a/backend/src/applications/files/services/files-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-manager.service.spec.ts
@@ -24,6 +24,7 @@ import * as untarUtils from '../utils/untar-file'
import * as filesUtils from '../utils/files'
import * as tarUtils from '../utils/tar-file'
import * as taskUtils from '../utils/tasks'
+import * as zipUtils from '../utils/zip-file'
import { FilesLockManager } from './files-lock-manager.service'
import { FilesManager } from './files-manager.service'
import { FilesQueries } from './files-queries.service'
@@ -201,6 +202,7 @@ describe(FilesManager.name, () => {
path.join(parentPath, `${taskUtils.taskTemporaryPrefix(cacheKey)}${path.basename(name)}`)
)
vi.spyOn(tarUtils, 'createTar').mockResolvedValue(undefined)
+ vi.spyOn(zipUtils, 'createZip').mockResolvedValue(undefined)
vi.spyOn(filesUtils, 'getMimeType').mockReturnValue('image-png')
vi.spyOn(spacesPermsUtils, 'canAccessToSpace').mockReturnValue(true)
vi.spyOn(spacesPermsUtils, 'haveSpaceEnvPermissions').mockReturnValue(true)
@@ -747,7 +749,8 @@ describe(FilesManager.name, () => {
run: (space: any) =>
service.compress(user, space, {
name: 'archive',
- extension: 'tar.gz',
+ extension: 'tar',
+ compression: false,
compressInDirectory: true,
files: [{ name: 'file.txt', path: '/data/users/john/files/file.txt' }]
} as any)
@@ -1118,7 +1121,8 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: { cacheKey: 'task-c', props: {} } })
const dto = {
name: 'archive',
- extension: 'tgz',
+ extension: 'tar',
+ compression: true,
compressInDirectory: false,
files: [
{ path: '/data/users/john/files/dir', name: 'dir', rootAlias: null },
@@ -1148,7 +1152,8 @@ describe(FilesManager.name, () => {
})
const dto = {
name: 'archive-trash',
- extension: 'tgz',
+ extension: 'tar',
+ compression: true,
compressInDirectory: false,
files: [{ path: '/data/users/john/trash/source.txt', name: 'source.txt', rootAlias: null }]
} as any
@@ -1176,7 +1181,8 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
const dto = {
name: 'archive',
- extension: 'tgz',
+ extension: 'tar',
+ compression: true,
compressInDirectory: false,
files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
} as any
@@ -1196,7 +1202,8 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
const dto = {
name: 'archive',
- extension: 'tgz',
+ extension: 'tar',
+ compression: true,
compressInDirectory: false,
files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
} as any
@@ -1218,6 +1225,7 @@ describe(FilesManager.name, () => {
const dto = {
name: 'archive',
extension: 'tar',
+ compression: false,
compressInDirectory: false,
files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
} as any
@@ -1246,6 +1254,7 @@ describe(FilesManager.name, () => {
const dto = {
name: 'archive',
extension: 'tar',
+ compression: false,
compressInDirectory: true,
files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
} as any
@@ -1254,6 +1263,32 @@ describe(FilesManager.name, () => {
expect(tarUtils.createTar).toHaveBeenCalledWith('/data/users/john/tmp/archive.tar-compress-uuid', dto.files, false, undefined, undefined, 60)
})
+
+ it('should create a compressed ZIP archive', async () => {
+ vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.zip')
+ vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.zip-compress-uuid')
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
+ const dto = {
+ name: 'archive.zip',
+ extension: 'zip',
+ compression: true,
+ compressInDirectory: false,
+ files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
+ } as any
+
+ await service.compress(user, space, dto)
+
+ expect(zipUtils.createZip).toHaveBeenCalledWith(
+ '/data/users/john/tmp/archive.zip-compress-uuid',
+ dto.files,
+ true,
+ undefined,
+ undefined,
+ undefined
+ )
+ expect(tarUtils.createTar).not.toHaveBeenCalled()
+ expect(filesUtils.moveFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive.zip-compress-uuid', '/tmp/archive.zip')
+ })
})
describe('decompress', () => {
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index 5f1957b6..fa66f9f0 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -19,7 +19,7 @@ import { canAccessToSpace, haveSpaceEnvPermissions } from '../../spaces/utils/pe
import { UserModel } from '../../users/models/user.model'
import { DEPTH, LOCK_DEPTH } from '../../webdav/constants/webdav'
import { CACHE_LOCK_FILE_TTL } from '../constants/cache'
-import { TAR_GZ_EXTENSION } from '../constants/compress'
+import { TAR_EXTENSION, TAR_GZ_EXTENSION, ZIP_EXTENSION } from '../constants/compress'
import { COMPRESSION_EXTENSION } from '../constants/files'
import { ALL_DOCUMENT_TYPES, DEFAULT_DOCUMENT_TYPES, SAMPLE_PATH_WITHOUT_EXT } from '../constants/samples'
import { CompressFileDto, DownloadFileDto } from '../dto/file-operations.dto'
@@ -65,6 +65,7 @@ import { FILE_ERROR_MESSAGES, maxFileSizeExceededError } from '../utils/errors'
import { createTaskTemporaryDir, taskTemporaryPath } from '../utils/tasks'
import { FilesTasksTransfer } from './tasks/files-tasks-transfer.service'
import { createTar } from '../utils/tar-file'
+import { createZip } from '../utils/zip-file'
@Injectable()
export class FilesManager {
@@ -629,7 +630,8 @@ export class FilesManager {
this.checkNotTrashRepository(space)
}
const srcPath = dirName(space.realPath)
- const archiveExt = dto.name.endsWith(dto.extension) ? '' : `.${dto.extension}`
+ const outputExtension = dto.extension === TAR_EXTENSION && dto.compression ? TAR_GZ_EXTENSION : dto.extension
+ const archiveExt = dto.name.endsWith(`.${outputExtension}`) ? '' : `.${outputExtension}`
const dstPath = await uniqueFilePathFromDir(path.join(dto.compressInDirectory ? srcPath : user.tasksPath, `${dto.name}${archiveExt}`))
const tmpPath = isTaskContext
? taskTemporaryPath(user.tasksPath, space.task!.cacheKey, dstPath)
@@ -653,14 +655,13 @@ export class FilesManager {
// do
try {
const maxArchiveSize = space.storageQuota === null ? undefined : Math.max(0, space.storageQuota - space.storageUsage)
- await createTar(
- tmpPath,
- dto.files.map((entry) => ({ ...entry, path: entry.path! })),
- dto.extension === TAR_GZ_EXTENSION,
- signal,
- isTaskContext ? this.filesTasksTransfer.createByteProgressHandler(space) : undefined,
- maxArchiveSize
- )
+ const entries = dto.files.map((entry) => ({ ...entry, path: entry.path! }))
+ const onProgress = isTaskContext ? this.filesTasksTransfer.createByteProgressHandler(space) : undefined
+ if (dto.extension === ZIP_EXTENSION) {
+ await createZip(tmpPath, entries, dto.compression, signal, onProgress, maxArchiveSize)
+ } else {
+ await createTar(tmpPath, entries, dto.compression, signal, onProgress, maxArchiveSize)
+ }
await moveFiles(tmpPath, dstPath)
} catch (e) {
await removeFiles(tmpPath).catch((err: Error) => this.logger.error({ tag: this.compress.name, msg: `unable to remove ${tmpPath} : ${err}` }))
diff --git a/backend/src/applications/files/services/files-methods.service.spec.ts b/backend/src/applications/files/services/files-methods.service.spec.ts
index 32daefee..f3347921 100644
--- a/backend/src/applications/files/services/files-methods.service.spec.ts
+++ b/backend/src/applications/files/services/files-methods.service.spec.ts
@@ -101,6 +101,7 @@ describe(FilesMethods.name, () => {
const compressFileDto: CompressFileDto = {
name: '../../archive',
compressInDirectory: false,
+ compression: false,
files: [{ name: '../../foo', rootAlias: undefined }],
extension: TAR_EXTENSION
}
diff --git a/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.html b/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.html
index b74f0220..f43a221d 100755
--- a/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.html
+++ b/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.html
@@ -15,15 +15,15 @@
<div class="modal-body">
<div class="input-group">
<input [(ngModel)]="archiveProps.name" appAutofocus class="form-control" [placeholder]="'Archive name' | translate:locale.language" type="text">
- <div class="input-group-text">
- <span style="min-width: 40px">.{{ archiveProps.extension }}</span>
- </div>
+ <select [(ngModel)]="archiveProps.extension" class="form-select flex-grow-0" style="width: 85px">
+ @for (extension of archiveExtensions; track extension) {
+ <option [ngValue]="extension">.{{ extensionLabel(extension) }}</option>
+ }
+ </select>
</div>
<div class="form-check mb-0 mt-3 ps-0">
<label class="form-check-label ps-1">
- <input [(ngModel)]="compression"
- (ngModelChange)="setCompression($event)"
- type="checkbox">
+ <input [(ngModel)]="archiveProps.compression" type="checkbox">
<span class="me-1" l10nTranslate>Enable compression</span>
<small l10nTranslate>(this may take longer)</small>
</label>
diff --git a/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.ts b/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.ts
index 0b15df6b..2ce35966 100644
--- a/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.ts
+++ b/frontend/src/app/applications/files/components/dialogs/files-compression-dialog.component.ts
@@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component, EventEmitter, HostListener, inject,
import { FormsModule } from '@angular/forms'
import { FaIconComponent } from '@fortawesome/angular-fontawesome'
import { faFileArchive } from '@fortawesome/free-solid-svg-icons'
-import { TAR_EXTENSION, TAR_GZ_EXTENSION } from '@sync-in-server/backend/src/applications/files/constants/compress'
+import { TAR_EXTENSION, TAR_GZ_EXTENSION, ZIP_EXTENSION } from '@sync-in-server/backend/src/applications/files/constants/compress'
import type { CompressFileDto } from '@sync-in-server/backend/src/applications/files/dto/file-operations.dto'
import { L10N_LOCALE, L10nLocale, L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
import { AutofocusDirective } from '../../../../common/directives/auto-focus.directive'
@@ -16,12 +16,18 @@ import { FilesService } from '../../services/files.service'
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FilesCompressionDialogComponent implements OnInit {
- @Input() archiveProps: CompressFileDto = { name: '', files: [], compressInDirectory: true, extension: TAR_EXTENSION }
+ @Input() archiveProps: CompressFileDto = {
+ name: '',
+ files: [],
+ compressInDirectory: true,
+ compression: false,
+ extension: TAR_EXTENSION
+ }
@Output() submitEvent = new EventEmitter()
public disableInDirCompression = false
protected readonly locale = inject<L10nLocale>(L10N_LOCALE)
protected readonly layout = inject(LayoutService)
- protected compression = false
+ protected readonly archiveExtensions: CompressFileDto['extension'][] = [TAR_EXTENSION, ZIP_EXTENSION]
protected readonly icons = { faFileArchive }
protected submitted = false
private readonly filesService = inject(FilesService)
@@ -46,7 +52,7 @@ export class FilesCompressionDialogComponent implements OnInit {
}
}
- setCompression(enable: boolean) {
- this.archiveProps.extension = enable ? TAR_GZ_EXTENSION : TAR_EXTENSION
+ extensionLabel(extension: CompressFileDto['extension']) {
+ return extension === TAR_EXTENSION && this.archiveProps.compression ? TAR_GZ_EXTENSION : extension
}
}
diff --git a/frontend/src/app/applications/files/components/sidebar/files-clipboard.component.ts b/frontend/src/app/applications/files/components/sidebar/files-clipboard.component.ts
index f5fc7c18..cb30b1e4 100644
--- a/frontend/src/app/applications/files/components/sidebar/files-clipboard.component.ts
+++ b/frontend/src/app/applications/files/components/sidebar/files-clipboard.component.ts
@@ -69,6 +69,7 @@ export class FilesClipboardComponent implements OnDestroy {
const archiveProps: CompressFileDto = {
name: this.files[0].name,
compressInDirectory: this.operations[this.selectedAction].operation === FILE_OPERATION.COMPRESS,
+ compression: false,
files: this.files.map((f: FileModel) => ({ name: f.name, rootAlias: f.root?.alias, path: f.path })),
extension: TAR_EXTENSION
}
diff --git a/frontend/src/app/applications/files/services/files.service.ts b/frontend/src/app/applications/files/services/files.service.ts
index 1f0b500b..2290416d 100644
--- a/frontend/src/app/applications/files/services/files.service.ts
+++ b/frontend/src/app/applications/files/services/files.service.ts
@@ -2,6 +2,7 @@ import type { TreeNode } from '@ali-hm/angular-tree-component'
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import { DomSanitizer } from '@angular/platform-browser'
+import { TAR_EXTENSION, TAR_GZ_EXTENSION } from '@sync-in-server/backend/src/applications/files/constants/compress'
import {
FILE_MODE,
FILE_OPERATION,
@@ -177,7 +178,8 @@ export class FilesService {
compress(op: CompressFileDto) {
const dirPath = this.currentRoute
- this.http.post<FileTask>(`${API_FILES_TASK_OPERATION_COMPRESS}/${dirPath}/${op.name}.${op.extension}`, op).subscribe({
+ const outputExtension = op.extension === TAR_EXTENSION && op.compression ? TAR_GZ_EXTENSION : op.extension
+ this.http.post<FileTask>(`${API_FILES_TASK_OPERATION_COMPRESS}/${dirPath}/${op.name}.${outputExtension}`, op).subscribe({
next: (t: FileTask) => this.filesTasksService.addTask(t),
error: (e: HttpErrorResponse) => this.layout.sendNotification('error', 'Compression failed', op.name, e)
})
diff --git a/frontend/src/app/applications/spaces/components/spaces-browser.component.ts b/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
index 4e3eed47..1edcc3ff 100644
--- a/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
+++ b/frontend/src/app/applications/spaces/components/spaces-browser.component.ts
@@ -614,6 +614,7 @@ export class SpacesBrowserComponent implements OnInit, AfterViewInit, OnDestroy
const archiveProps: CompressFileDto = {
name: this.selection[0].name,
compressInDirectory: this.isTrashRepo || this.inSharesList ? false : inDir,
+ compression: false,
files: this.selection.map((f: FileModel) => ({ name: f.name, rootAlias: f.root?.alias })),
extension: TAR_EXTENSION
}