var __decorate = (this && this.__decorate) || function (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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import DatabaseService from "./DatabaseService";
import NotAuthorizedException from "../../Types/Exception/NotAuthorizedException";
import File from "../../Models/DatabaseModels/File";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import crypto from "crypto";
const generateImageAccessToken = () => {
    return crypto.randomBytes(32).toString("hex");
};
export class Service extends DatabaseService {
    constructor() {
        super(File);
    }
    async onBeforeCreate(createBy) {
        /*
         * Always generate an unguessable access token server-side. The token
         * is the only safe way to address an inline-uploaded image in
         * markdown without exposing the enumerable ObjectID.
         */
        if (!createBy.data.imageAccessToken) {
            createBy.data.imageAccessToken = generateImageAccessToken();
        }
        return { createBy, carryForward: null };
    }
    async onBeforeUpdate(updateBy) {
        if (!updateBy.props.isRoot) {
            throw new NotAuthorizedException("Not authorized to update a file.");
        }
        return { updateBy, carryForward: null };
    }
    async onBeforeDelete(deleteBy) {
        if (!deleteBy.props.isRoot) {
            throw new NotAuthorizedException("Not authorized to delete a file.");
        }
        return { deleteBy, carryForward: null };
    }
    async onBeforeFind(findBy) {
        if (!findBy.props.isRoot) {
            findBy.query = Object.assign(Object.assign({}, findBy.query), { isPublic: true });
        }
        return { findBy, carryForward: null };
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeCreate", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeUpdate", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeDelete", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeFind", null);
export default new Service();
//# sourceMappingURL=FileService.js.map