UNPKG

ravendb

Version:
84 lines 3.02 kB
import { getEtagHeader } from "../../../Utility/HttpUtil.js"; import { AttachmentResult } from "../../Attachments/index.js"; import { RavenCommand } from "../../../Http/RavenCommand.js"; import { throwError } from "../../../Exceptions/index.js"; import { StringUtil } from "../../../Utility/StringUtil.js"; export class GetAttachmentOperation { _documentId; _name; _type; _changeVector; get resultType() { return "CommandResult"; } constructor(documentId, name, type, changeVector) { this._documentId = documentId; this._name = name; this._type = type; this._changeVector = changeVector; } getCommand(store, conventions, cache) { return new GetAttachmentCommand(this._documentId, this._name, this._type, this._changeVector); } } export class GetAttachmentCommand extends RavenCommand { _documentId; _name; _type; _changeVector; constructor(documentId, name, type, changeVector) { super(); this.result = null; if (StringUtil.isNullOrWhitespace(documentId)) { throwError("InvalidArgumentException", "DocumentId cannot be null or empty"); } if (StringUtil.isNullOrWhitespace(name)) { throwError("InvalidArgumentException", "Name cannot be null or empty"); } if (type !== "Document" && !changeVector) { throwError("InvalidArgumentException", "Change vector cannot be null for attachment type " + type); } this._documentId = documentId; this._name = name; this._type = type; this._changeVector = changeVector; this._responseType = "Empty"; } createRequest(node) { const uri = node.url + "/databases/" + node.database + "/attachments?id=" + encodeURIComponent(this._documentId) + "&name=" + encodeURIComponent(this._name); if (this._type !== "Document") { const body = this._serializer.serialize({ Type: this._type, ChangeVector: this._changeVector }); return { uri, method: "POST", body }; } return { uri }; } async processResponse(cache, response, bodyStream, url) { const contentType = response.headers.get("content-type"); const changeVector = getEtagHeader(response); const hash = response.headers.get("attachment-hash"); let size = 0; const sizeHeader = response.headers.get("attachment-size"); if (sizeHeader) { size = Number.parseInt(sizeHeader, 10); } const details = { name: this._name, documentId: this._documentId, contentType, hash, changeVector, size }; this.result = new AttachmentResult(bodyStream, details, response); return "Manually"; } get isReadRequest() { return true; } } //# sourceMappingURL=GetAttachmentOperation.js.map