Autodesk.Viewing.Document.load() documentId / urn structure

huangapple go评论69阅读模式
英文:

Autodesk.Viewing.Document.load() documentId / urn structure

问题

我使用了Autodesk.Viewing.Document.load()函数 点击。文档指明第一个参数是文件的 URN。然而,如果我使用 js-base64 模块 点击 将 URN(urn:...)编码为 Base64(使用 encodeURI),然后调用

Autodesk.Viewing.Document.load(base64_decoded_urn, onSuccessCallback, onErrorCallback)

我从服务器收到 400 响应。

但是,当我调用

Autodesk.Viewing.Document.load('urn:' + base64_decoded_urn, onSuccessCallback, onErrorCallback)

文档可以被获取。然而,我实际上是使用 urn:urn:... 调用 load 函数,因为编码的 base64 URN 仍然包含前缀 'urn:'。

这种行为是否符合预期?

英文:

I am using the Autodesk.Viewing.Document.load() function click. The documentation states that the first parameter is the urn of the file. However if I encode a urn (urn:...) to Base64 (encodeURI) using the js-base64 module click and call

Autodesk.Viewing.Document.load(base64_decoded_urn, onSuccessCallback, onErrorCallback)

I get a 400 response from the server.

But when I call

Autodesk.Viewing.Document.load('urn:' + base64_decoded_urn, onSuccessCallback, onErrorCallback)

The document can be fetched. However, I am actually calling the load function with urn:urn:... because the encoded base64 urn still includes the prefix 'urn:'

Is this behavior desired?

答案1

得分: 0

这是正确的方式。
对于编码,你可以依赖于 urnify 方法:

service.urnify = (id) => Buffer.from(id).toString('base64').replace(/=/g, '');

就像我们在我们的 Simple Viewer 教程 中所做的一样。

在这种情况下,我们在 urn 前面添加了 urn(参考 viewer.js):

export function loadModel(viewer, urn) {
    return new Promise(function (resolve, reject) {
        function onDocumentLoadSuccess(doc) {
            resolve(viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry()));
        }
        function onDocumentLoadFailure(code, message, errors) {
            reject({ code, message, errors });
        }
        viewer.setLightPreset(0);
        Autodesk.Viewing.Document.load('urn:' + urn, onDocumentLoadSuccess, onDocumentLoadFailure);
    });
}

而传递给 loadModel 函数的 urn 值是经过 base64 编码的,正如我们在 '/api/models' 终端点中看到的(参考 这里):

router.post('/api/models', formidable(), async function (req, res, next) {
    const file = req.files['model-file'];
    if (!file) {
        res.status(400).send('The required field ("model-file") is missing.');
        return;
    }
    try {
        const obj = await uploadObject(file.name, file.path);
        await translateObject(urnify(obj.objectId), req.fields['model-zip-entrypoint']);
        res.json({
            name: obj.objectKey,
            urn: urnify(obj.objectId)
        });
    } catch (err) {
        next(err);
    }
});
英文:

Yes, this is the correct way.
For encoding you can rely on urnify method:

service.urnify = (id) => Buffer.from(id).toString('base64').replace(/=/g, '');

just like we do in our Simple Viewer tutorial.

In this case, we're prefixing the urn with urn (refer to viewer.js):

export function loadModel(viewer, urn) {
    return new Promise(function (resolve, reject) {
        function onDocumentLoadSuccess(doc) {
            resolve(viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry()));
        }
        function onDocumentLoadFailure(code, message, errors) {
            reject({ code, message, errors });
        }
        viewer.setLightPreset(0);
        Autodesk.Viewing.Document.load('urn:' + urn, onDocumentLoadSuccess, onDocumentLoadFailure);
    });
}

And the value of the urn passed to loadModel function is base64-encoded, as we can see at '/api/models'endpoint (refer here):

router.post('/api/models', formidable(), async function (req, res, next) {
    const file = req.files['model-file'];
    if (!file) {
        res.status(400).send('The required field ("model-file") is missing.');
        return;
    }
    try {
        const obj = await uploadObject(file.name, file.path);
        await translateObject(urnify(obj.objectId), req.fields['model-zip-entrypoint']);
        res.json({
            name: obj.objectKey,
            urn: urnify(obj.objectId)
        });
    } catch (err) {
        next(err);
    }
});

huangapple
  • 本文由 发表于 2023年5月10日 22:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定