UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

169 lines (168 loc) 7.13 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "WebDAVSpaces", { enumerable: true, get: function() { return WebDAVSpaces; } }); const _common = require("@nestjs/common"); const _files = require("../../files/utils/files"); const _spaces = require("../../spaces/constants/spaces"); const _spacesbrowserservice = require("../../spaces/services/spaces-browser.service"); const _spacesmanagerservice = require("../../spaces/services/spaces-manager.service"); const _permissions = require("../../spaces/utils/permissions"); const _routes = require("../constants/routes"); const _webdav = require("../constants/webdav"); const _webdavfilemodel = require("../models/webdav-file.model"); const _routes1 = require("../utils/routes"); function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } let WebDAVSpaces = class WebDAVSpaces { async spaceEnv(user, path) { try { const space = await this.spacesManager.spaceEnv(user, (0, _routes1.WEBDAV_PATH_TO_SPACE_SEGMENTS)(path)); if (space) { return space; } } catch (e) { this.logger.warn(`${this.spaceEnv.name} : ${e}`); return null; } this.logger.warn(`Space not authorized or not found : ${path}`); return null; } propfind(req, space) { switch(space){ case _spaces.SPACE_REPOSITORY.FILES: return this.listFiles(req, req.space); case _routes.WEBDAV_NS.SERVER: return this.listServer(req); case _routes.WEBDAV_NS.WEBDAV: return this.listWebDAV(req); case _routes.WEBDAV_NS.SPACES: return this.listSpaces(req); case _routes.WEBDAV_NS.TRASH: return this.listTrashes(req); default: this.logger.error(`Unknown space ${space}`); throw new _common.HttpException('Unknown space', _common.HttpStatus.NOT_FOUND); } } async *listServer(req) { yield this.roots[_routes.WEBDAV_NS.SERVER]; if (req.dav.depth === _webdav.DEPTH.MEMBERS) { yield this.roots[_routes.WEBDAV_NS.WEBDAV]; } } async *listWebDAV(req) { yield this.roots[_routes.WEBDAV_NS.WEBDAV]; if (req.dav.depth === _webdav.DEPTH.MEMBERS) { for (const root of Object.values(this.roots)){ if ([ _routes.WEBDAV_NS.SERVER, _routes.WEBDAV_NS.WEBDAV ].indexOf(root.name) === -1) { // filter repositories based on user applications if (!(0, _permissions.canAccessToSpaceUrl)(req.user, _routes.WEBDAV_SPACES[root.name].spaceRepository)) { continue; } yield root; } } } } async *listSpaces(req) { yield this.roots[_routes.WEBDAV_NS.SPACES]; if (req.dav.depth === _webdav.DEPTH.MEMBERS) { for (const s of (await this.spacesManager.listSpaces(req.user.id))){ yield new _webdavfilemodel.WebDAVFile({ id: s.id, name: s.name, alias: s.alias, isDir: true, size: 0, mime: undefined, ctime: new Date(s.createdAt).getTime(), mtime: new Date(s.modifiedAt).getTime() }, req.dav.url); } } } async *listTrashes(req) { yield this.roots[_routes.WEBDAV_NS.TRASH]; if (req.dav.depth === _webdav.DEPTH.MEMBERS) { for (const f of (await this.spacesManager.listTrashes(req.user))){ yield new _webdavfilemodel.WebDAVFile({ id: f.id, alias: f.alias, name: `${f.alias} (${f.nb})`, isDir: true, size: 0, mime: undefined, mtime: f.mtime, ctime: f.ctime }, req.dav.url); } } } async *listFiles(req, space) { let isDir; if (space.inSharesList) { // The shares are defined like a files (direct link) isDir = true; yield this.roots[_routes.WEBDAV_NS.SHARES]; } else { if (!await (0, _files.isPathExists)(space.realPath)) { this.logger.warn(`Location not found : ${space.realPath}`); throw new _common.HttpException('Location not found', _common.HttpStatus.NOT_FOUND); } yield new _webdavfilemodel.WebDAVFile(await (0, _files.getProps)(space.realPath, req.dav.url), req.dav.url, true); } if (req.dav.depth === _webdav.DEPTH.MEMBERS && (isDir === true || await (0, _files.isPathIsDir)(space.realPath))) { const { files } = await this.spacesBrowser.browse(req.user, space); for (const f of files){ yield new _webdavfilemodel.WebDAVFile(f, req.dav.url); } } } constructor(spacesManager, spacesBrowser){ this.spacesManager = spacesManager; this.spacesBrowser = spacesBrowser; this.logger = new _common.Logger(WebDAVSpaces.name); this.roots = {}; for (const [name, info] of Object.entries(_routes.WEBDAV_SPACES)){ this.roots[name] = new _webdavfilemodel.WebDAVFile({ id: 0, name: name, isDir: true, size: 0, ctime: new Date().getTime(), mtime: new Date().getTime(), mime: undefined }, info.route, true); } } }; WebDAVSpaces = _ts_decorate([ (0, _common.Injectable)(), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _spacesmanagerservice.SpacesManager === "undefined" ? Object : _spacesmanagerservice.SpacesManager, typeof _spacesbrowserservice.SpacesBrowser === "undefined" ? Object : _spacesbrowserservice.SpacesBrowser ]) ], WebDAVSpaces); //# sourceMappingURL=webdav-spaces.service.js.map