Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
import {
Body,
Controller,
Copy,
Delete,
Get,
Header,
HttpException,
HttpStatus,
Move,
Param,
Post,
Proppatch,
Put,
Query,
Req,
Res,
StreamableFile,
UseGuards,
UseInterceptors
} from '@nestjs/common'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { AuthTokenSkip } from '../../authentication/decorators/auth-token-skip.decorator.js'
import { AuthRateLimitGuard } from '../../authentication/guards/auth-rate-limit.guard.js'
import { type FastifyAuthenticatedRequest } from '../../authentication/interfaces/auth-request.interface.js'
import { integerFromStringSchema } from '../../configuration/config.schema.js'
import { SkipSpacePermissionsCheck } from '../spaces/decorators/space-skip-permissions.decorator.js'
import { type FastifySpaceRequest } from '../spaces/interfaces/space-request.interface.js'
import { USER_PERMISSION } from '../users/constants/user.js'
import { UserHavePermission } from '../users/decorators/permissions.decorator.js'
import { GetUser } from '../users/decorators/user.decorator.js'
import { UserPermissionsGuard } from '../users/guards/permissions.guard.js'
import { UserModel } from '../users/models/user.model.js'
import { CLIENT_AUTH_TYPE } from './constants/auth.js'
import { SYNC_ROUTE } from './constants/routes.js'
import { CHECK_SERVER_RESP, SYNC_IN_SERVER_AGENT } from './constants/sync.js'
import { SyncEnvironment } from './decorators/sync-environment.decorator.js'
import { type SyncClientAuthDto, syncClientAuthSchema } from './dtos/sync-client-auth.dto.js'
import {
type SyncClientAuthRegistrationDto,
syncClientAuthRegistrationSchema,
type SyncClientRegistrationDto,
syncClientRegistrationSchema
} from './dtos/sync-client-registration.dto.js'
import {
type SyncCopyMoveDto,
syncCopyMoveSchema,
type SyncDiffDto,
syncDiffSchema,
type SyncMakeDto,
syncMakeSchema,
type SyncPropsDto,
syncPropsSchema
} from './dtos/sync-operations.dto.js'
import { type SyncPathDto, syncPathSchema, syncPathsSchema, type SyncPathUpdateDto, syncPathUpdateSchema } from './dtos/sync-path.dto.js'
import { type SyncUploadDto, syncUploadSchema } from './dtos/sync-upload.dto.js'
import { SyncDiffGzipBodyInterceptor } from './interceptors/sync-diff-gzip-body.interceptor.js'
import { AppStoreManifest } from './interfaces/store-manifest.interface.js'
import { SyncClientAuthCookie, SyncClientAuthRegistration, SyncClientAuthToken } from './interfaces/sync-client-auth.interface.js'
import { SyncClientPaths } from './interfaces/sync-client-paths.interface.js'
import { ExistingSyncPathDto, SyncPathsDiff, SyncPathSettings } from './interfaces/sync-path.interface.js'
import { SyncClientsManager } from './services/sync-clients-manager.service.js'
import { SyncManager } from './services/sync-manager.service.js'
import { SyncPathsManager } from './services/sync-paths-manager.service.js'
import { clientAuthTypeParameterSchema, clientIdParameterSchema } from './sync.schema.js'
@Controller(SYNC_ROUTE.BASE)
export class SyncController {
constructor(
private readonly syncManager: SyncManager,
private readonly syncClientsManager: SyncClientsManager,
private readonly syncPathsManager: SyncPathsManager
) {}
/* CLIENT */
@Get(SYNC_ROUTE.HANDSHAKE)
@AuthTokenSkip()
handshake(@Req() req: FastifyRequest) {
if ('user-agent' in req.headers && (req.headers['user-agent'] as string).startsWith(SYNC_IN_SERVER_AGENT)) {
return CHECK_SERVER_RESP
}
throw new HttpException('Server not found', HttpStatus.NOT_FOUND)
}
@Post(SYNC_ROUTE.REGISTER)
@Header('Cache-Control', 'no-store')
@AuthTokenSkip()
@UseGuards(AuthRateLimitGuard)
register(
@Body({ schema: syncClientRegistrationSchema }) syncClientRegistrationDto: SyncClientRegistrationDto,
@Req() req: FastifyRequest
): Promise<SyncClientAuthRegistration> {
return this.syncClientsManager.register(syncClientRegistrationDto, req.ip)
}
@Post(SYNC_ROUTE.REGISTER_AUTH)
@Header('Cache-Control', 'no-store')
@UserHavePermission(USER_PERMISSION.DESKTOP_APP)
@UseGuards(UserPermissionsGuard)
registerWithAuth(
@Body({ schema: syncClientAuthRegistrationSchema }) clientAuthenticatedRegistrationDto: SyncClientAuthRegistrationDto,
@Req() req: FastifyAuthenticatedRequest
): Promise<SyncClientAuthRegistration> {
return this.syncClientsManager.registerWithAuth(clientAuthenticatedRegistrationDto, req)
}
@Post(SYNC_ROUTE.UNREGISTER)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP)
@UseGuards(UserPermissionsGuard)
unregister(@GetUser() user: UserModel): Promise<void> {
return this.syncClientsManager.unregister(user)
}
@Get(SYNC_ROUTE.APP_STORE)
@AuthTokenSkip()
checkAppStore(): Promise<AppStoreManifest | null> {
// This route must be public to allow clients to receive updates
return this.syncClientsManager.checkAppStore()
}
@Post(`${SYNC_ROUTE.AUTH}/:type`)
@Header('Cache-Control', 'no-store')
@AuthTokenSkip()
@UseGuards(AuthRateLimitGuard)
authenticate(
@Param('type', { schema: clientAuthTypeParameterSchema }) type: CLIENT_AUTH_TYPE,
@Body({ schema: syncClientAuthSchema }) clientAuthDto: SyncClientAuthDto,
@Req() req: FastifyRequest,
@Res({ passthrough: true }) res: FastifyReply
): Promise<SyncClientAuthCookie | SyncClientAuthToken> {
return this.syncClientsManager.authenticate(type, clientAuthDto, req.ip, res)
}
@Get(SYNC_ROUTE.CLIENTS)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP)
@UseGuards(UserPermissionsGuard)
getClients(@GetUser() user: UserModel): Promise<SyncClientPaths[]> {
return this.syncClientsManager.getClients(user)
}
@Delete(`${SYNC_ROUTE.CLIENTS}/:id`)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP)
@UseGuards(UserPermissionsGuard)
deleteClient(@GetUser() user: UserModel, @Param('id', { schema: clientIdParameterSchema }) clientId: string): Promise<void> {
return this.syncClientsManager.deleteClient(user, clientId)
}
/* PATHS */
@Delete(`${SYNC_ROUTE.CLIENTS}/:id/${SYNC_ROUTE.PATHS}/:pathId`)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP_SYNC)
@UseGuards(UserPermissionsGuard)
deleteClientPath(
@GetUser() user: UserModel,
@Param('id', { schema: clientIdParameterSchema }) clientId: string,
@Param('pathId', { schema: integerFromStringSchema }) pathId: number
): Promise<void> {
return this.syncPathsManager.deletePath(user, pathId, clientId)
}
@Put(`${SYNC_ROUTE.CLIENTS}/:id/${SYNC_ROUTE.PATHS}/:pathId`)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP_SYNC)
@UseGuards(UserPermissionsGuard)
updatePath(
@GetUser() user: UserModel,
@Param('id', { schema: clientIdParameterSchema }) clientId: string,
@Param('pathId', { schema: integerFromStringSchema }) pathId: number,
@Body({ schema: syncPathUpdateSchema }) syncPathUpdateDto: SyncPathUpdateDto
): Promise<SyncPathSettings> {
return this.syncPathsManager.updatePath(user, clientId, pathId, syncPathUpdateDto)
}
@Post(`${SYNC_ROUTE.PATHS}/*`)
@SkipSpacePermissionsCheck()
@SyncEnvironment()
createPath(
@Req() req: FastifySpaceRequest,
@Body({ schema: syncPathSchema }) syncPathDto: SyncPathDto
): Promise<{
id: number
permissions: string
}> {
return this.syncPathsManager.createPath(req, syncPathDto)
}
@Delete(`${SYNC_ROUTE.PATHS}/:id`)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP_SYNC)
@UseGuards(UserPermissionsGuard)
deletePath(@GetUser() user: UserModel, @Param('id', { schema: integerFromStringSchema }) pathId: number): Promise<void> {
return this.syncPathsManager.deletePath(user, pathId)
}
@Put(SYNC_ROUTE.PATHS)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP_SYNC)
@UseGuards(UserPermissionsGuard)
updatePaths(@GetUser() user: UserModel, @Body({ schema: syncPathsSchema }) syncPathsDto: ExistingSyncPathDto[]): Promise<SyncPathsDiff> {
return this.syncPathsManager.updatePaths(user, syncPathsDto)
}
/* OPERATIONS */
@Post(`${SYNC_ROUTE.OPERATION}/${SYNC_ROUTE.DIFF}/:id`)
@UserHavePermission(USER_PERMISSION.DESKTOP_APP_SYNC)
@UseGuards(UserPermissionsGuard)
@UseInterceptors(SyncDiffGzipBodyInterceptor)
diff(
@GetUser() user: UserModel,
@Param('id', { schema: integerFromStringSchema }) pathId: number,
@Body({ schema: syncDiffSchema }) syncDiffDto: SyncDiffDto,
@Res({ passthrough: true }) res: FastifyReply
): Promise<void> {
return this.syncManager.diff(user, pathId, syncDiffDto, res)
}
@Get(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
download(@Req() req: FastifySpaceRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<StreamableFile> {
return this.syncManager.download(req, res)
}
@Post(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
uploadCreate(@Req() req: FastifySpaceRequest, @Query({ schema: syncUploadSchema }) syncUploadDto: SyncUploadDto): Promise<{ ino: number }> {
return this.syncManager.upload(req, syncUploadDto)
}
@Put(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
uploadOverwrite(@Req() req: FastifySpaceRequest, @Query({ schema: syncUploadSchema }) syncUploadDto: SyncUploadDto): Promise<{ ino: number }> {
return this.syncManager.upload(req, syncUploadDto)
}
@Post(`${SYNC_ROUTE.OPERATION}/${SYNC_ROUTE.MAKE}/*`)
@SyncEnvironment()
make(@Req() req: FastifySpaceRequest, @Body({ schema: syncMakeSchema }) syncMakeDto: SyncMakeDto): Promise<{ ino: number }> {
return this.syncManager.make(req, syncMakeDto)
}
@Proppatch(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
props(@Req() req: FastifySpaceRequest, @Body({ schema: syncPropsSchema }) syncPropsDto: SyncPropsDto): Promise<void> {
return this.syncManager.props(req, syncPropsDto)
}
@Move(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
move(@Req() req: FastifySpaceRequest, @Body({ schema: syncCopyMoveSchema }) syncCopyMoveDto: SyncCopyMoveDto): Promise<void> {
return this.syncManager.copyMove(req, syncCopyMoveDto, true)
}
@Copy(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
copy(
@Req() req: FastifySpaceRequest,
@Body({ schema: syncCopyMoveSchema }) syncCopyMoveDto: SyncCopyMoveDto
): Promise<{ ino: number; mtime: number }> {
return this.syncManager.copyMove(req, syncCopyMoveDto, false)
}
@Delete(`${SYNC_ROUTE.OPERATION}/*`)
@SyncEnvironment()
delete(@Req() req: FastifySpaceRequest): Promise<void> {
return this.syncManager.delete(req)
}
}