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 {
API_FILES_OPERATION,
API_FILES_OPERATION_THUMBNAIL,
API_FILES_TASK_OPERATION
} from '@sync-in-server/backend/src/applications/files/constants/routes'
import type { FileLockProps, FileProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
import { COLLABORA_ONLINE_EXTENSIONS } from '@sync-in-server/backend/src/applications/files/editors/collabora-online/collabora-online.constants'
import { DRAWIO_SUPPORTED_EXTENSIONS } from '@sync-in-server/backend/src/applications/files/editors/drawio/drawio.constants'
import type { FileEditorProviders } from '@sync-in-server/backend/src/applications/files/editors/file-editor-providers.interface'
import { ONLY_OFFICE_EXTENSIONS } from '@sync-in-server/backend/src/applications/files/editors/only-office/only-office.constants'
import type { File } from '@sync-in-server/backend/src/applications/files/schemas/file.interface'
import { SPACE_OPERATION } from '@sync-in-server/backend/src/applications/spaces/constants/spaces'
import { currentTimeStamp, encodeUrl, popFromObject } from '@sync-in-server/backend/src/common/shared'
import type { Observable } from 'rxjs'
import { convertBytesToText, getNewly } from '../../../common/utils/functions'
import { dJs } from '../../../common/utils/time'
import { CommentModel } from '../../comments/models/comment.model'
import { SPACES_PERMISSIONS_TEXT } from '../../spaces/spaces.constants'
import { setTextIconPermissions } from '../../spaces/spaces.functions'
import type { OwnerType } from '../../users/interfaces/owner.interface'
import { userAvatarUrl } from '../../users/user.functions'
import {
COMPRESSIBLE_MIMES,
defaultMimeUrl,
getAssetsMimeUrl,
mimeDirectory,
mimeDirectoryShare,
mimeFile,
SHORT_MIME,
UNSUPPORTED_VIEW_EXTENSIONS
} from '../files.constants'
export class FileModel implements File {
id: number
ownerId: number
spaceId: number
spaceExternalRootId: number
shareExternalId: number
path: string
name: string
isDir: boolean
inTrash: boolean
mime: string
size: number
mtime: number
ctime: number
// Extra properties
readonly basePath: string
hasComments: boolean
isFavorite = false
root?: {
id: number
alias: string
owner: OwnerType
permissions: string
hPerms: Partial<typeof SPACES_PERMISSIONS_TEXT>
// only for shares
enabled?: boolean
description?: string
}
lock?: FileLockProps
shares: { id: number; alias: string; name: string; type: number }[] = []
links: { id: number; alias: string; name: string; type: number }[] = []
spaces: { id: number; alias: string; name: string }[] = []
syncs: { clientId: string; clientName: string; id: number }[] = []
comments: CommentModel[]
// Computed
shortMime: (typeof SHORT_MIME)[keyof typeof SHORT_MIME]
mimeUrl: string
hSize: string
hTimeAgo: string
dirSize: Observable<number | undefined>
nbBadges = 0
galleryBadges: ('lock' | 'favorites' | 'shares' | 'spaces' | 'links' | 'syncs' | 'comments')[] = []
// States
newly = 0
isRenamed = false
isImage = false
isViewable = false
isEditable = false
isTextProbeRequired = false
isCompressible = true
isBeingDeleted = false
isSelected = false
isDisabled = false
canBeReShared = false
haveThumbnail = false
constructor(props: FileProps | File, basePath: string, inShare = false, editorConfig: FileEditorProviders) {
this.setShares(popFromObject('shares', props))
Object.assign(this, props)
this.basePath = basePath
this.path = `${basePath}/${this.path !== '.' ? `${this.path}/` : ''}${this.root?.alias || this.name}`
this.mime = this.getMime(this.mime, inShare, editorConfig)
this.updateHTimeAgo(this.mtime)
this.setMimeUrl()
this.setHSize()
this.setRoot(inShare)
this.updateNbBadges()
}
get encodedPath(): string {
return encodeUrl(this.path)
}
get dataUrl(): string {
return `${API_FILES_OPERATION}/${this.encodedPath}`
}
get taskUrl(): string {
return `${API_FILES_TASK_OPERATION}/${this.encodedPath}`
}
get thumbnailUrl(): string {
return `${API_FILES_OPERATION_THUMBNAIL}/${this.encodedPath}`
}
fallBackMimeUrl() {
this.mimeUrl = defaultMimeUrl
}
rename(name: string) {
this.name = name
this.path = [...this.path.split('/').slice(0, -1), this.name].join('/')
}
updateMime(mime: string, inShare: boolean, editorConfig: FileEditorProviders) {
this.resetMimeState()
this.mime = this.getMime(mime, inShare, editorConfig)
this.setMimeUrl()
}
updateHTimeAgo(mtime?: number) {
mtime ??= currentTimeStamp(null, true)
this.hTimeAgo = dJs(mtime).fromNow(true)
this.newly = getNewly(mtime)
}
updateSize(size: number) {
this.size = size
this.setHSize()
}
createLock(lock: FileLockProps) {
this.lock = lock
}
removeLock() {
this.lock = null
}
getExtension(): string {
const dot = this.name.lastIndexOf('.')
return dot >= 0 ? this.name.slice(dot + 1).toLowerCase() : ''
}
updateNbBadges() {
this.galleryBadges = []
if (this.lock) this.galleryBadges.push('lock')
if (this.isFavorite) this.galleryBadges.push('favorites')
if (this.shares.length) this.galleryBadges.push('shares')
if (this.spaces.length) this.galleryBadges.push('spaces')
if (this.links.length) this.galleryBadges.push('links')
if (this.syncs.length) this.galleryBadges.push('syncs')
if (this.hasComments) this.galleryBadges.push('comments')
this.nbBadges = this.galleryBadges.length
}
private getType(inShare: boolean): 'directory_share' | 'directory' | 'file' {
return this.isDir ? (inShare ? mimeDirectoryShare : mimeDirectory) : mimeFile
}
private getMime(mime: string, inShare: boolean, editorConfig: FileEditorProviders): string {
if (this.isDir) {
this.isViewable = false
return this.getType(inShare)
}
const extension = this.getExtension()
const dash = mime?.indexOf('-') ?? -1
const temporaryMime = mime && dash >= 0 ? mime.slice(0, dash) : mime
if (editorConfig.drawio === true && DRAWIO_SUPPORTED_EXTENSIONS.has(extension)) {
this.shortMime = SHORT_MIME.DIAGRAM
this.isEditable = true
this.isViewable = true
return mime || mimeFile
}
if (extension === SHORT_MIME.PDF) {
this.shortMime = SHORT_MIME.PDF
this.isViewable = true
this.isEditable = editorConfig.onlyoffice === true || editorConfig.eurooffice === true
return mime || mimeFile
}
if (
(editorConfig.collabora === true && COLLABORA_ONLINE_EXTENSIONS.has(extension)) ||
((editorConfig.onlyoffice === true || editorConfig.eurooffice === true) && ONLY_OFFICE_EXTENSIONS.has(extension))
) {
this.shortMime = SHORT_MIME.DOCUMENT
this.isEditable = true
this.isViewable = true
return mime || mimeFile
}
if ((!mime || mime === mimeFile) && UNSUPPORTED_VIEW_EXTENSIONS.has(extension)) {
return this.getType(inShare)
}
if (!mime || mime === mimeFile) {
this.isViewable = true
this.isEditable = true
this.isTextProbeRequired = true
this.shortMime = SHORT_MIME.TEXT
return this.getType(inShare)
}
if (mime.includes(SHORT_MIME.MARKDOWN)) {
this.shortMime = SHORT_MIME.MARKDOWN
this.isViewable = true
this.isEditable = true
return mime
}
if (extension === 'mp4') {
this.isViewable = true
this.shortMime = SHORT_MIME.MEDIA
this.haveThumbnail = true
return mime
}
if (temporaryMime === SHORT_MIME.IMAGE) {
this.shortMime = SHORT_MIME.IMAGE
this.isImage = true
this.isViewable = true
this.haveThumbnail = true
return mime
}
if (temporaryMime === 'video' || temporaryMime === 'audio') {
this.shortMime = SHORT_MIME.MEDIA
this.isViewable = true
this.haveThumbnail = true
return mime
}
if (COMPRESSIBLE_MIMES.has(mime)) {
this.isCompressible = false
this.isViewable = false
return mime
}
if (!UNSUPPORTED_VIEW_EXTENSIONS.has(extension)) {
this.shortMime = SHORT_MIME.TEXT
this.isViewable = true
this.isEditable = true
this.isTextProbeRequired = temporaryMime !== SHORT_MIME.TEXT
}
return mime
}
private resetMimeState() {
this.shortMime = undefined
this.isImage = false
this.isViewable = false
this.isEditable = false
this.isTextProbeRequired = false
this.isCompressible = true
this.haveThumbnail = false
}
private setMimeUrl() {
this.mimeUrl = getAssetsMimeUrl(this.mime)
}
private setRoot(inShare: boolean) {
if (this.root) {
if (this.root.enabled === false) {
this.isDisabled = true
}
this.root.hPerms = setTextIconPermissions(this.root.permissions, this.isDir ? [] : [SPACE_OPERATION.DELETE, SPACE_OPERATION.ADD])
if (this.root?.owner?.login) {
this.root.owner.avatarUrl = userAvatarUrl(this.root.owner.login)
}
this.canBeReShared = inShare && SPACE_OPERATION.SHARE_OUTSIDE in this.root.hPerms
}
}
private setHSize() {
this.hSize = this.isDir && this.size === 0 ? '●' : convertBytesToText(this.size, 0, true)
}
private setShares(shares: { id: number; alias: string; name: string; type: number }[]) {
if (shares?.length) {
for (const s of shares) {
if (s.type === 0) {
this.shares.push(s)
} else {
this.links.push(s)
}
}
this.updateNbBadges()
}
}
}