Skip to content

Commit

Permalink
[feature] Add saveAsPath param to wopi saveCopyAs request
Browse files Browse the repository at this point in the history
  • Loading branch information
konovalovsergey committed Aug 6, 2024
1 parent f878e94 commit c4022a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
8 changes: 8 additions & 0 deletions Common/sources/commondefines.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function InputCommand(data, copyExplicit) {
this['attempt'] = data['attempt'];
this['convertToOrigin'] = data['convertToOrigin'];
this['isSaveAs'] = data['isSaveAs'];
this['saveAsPath'] = data['saveAsPath'];
if (copyExplicit) {
this['withAuthorization'] = data['withAuthorization'];
this['externalChangeInfo'] = data['externalChangeInfo'];
Expand Down Expand Up @@ -174,6 +175,7 @@ function InputCommand(data, copyExplicit) {
this['convertToOrigin'] = undefined;
this['originformat'] = undefined;
this['isSaveAs'] = undefined;
this['saveAsPath'] = undefined;
}
}
InputCommand.prototype = {
Expand Down Expand Up @@ -516,6 +518,12 @@ InputCommand.prototype = {
},
setIsSaveAs: function(data) {
this['isSaveAs'] = data;
},
getSaveAsPath: function() {
return this['saveAsPath'];
},
setSaveAsPath: function(data) {
this['saveAsPath'] = data;
}
};

Expand Down
15 changes: 11 additions & 4 deletions DocService/sources/canvasservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -1790,15 +1790,18 @@ exports.saveFromChanges = function(ctx, docId, statusInfo, optFormat, opt_userId
};

async function processWopiSaveAs(ctx, cmd) {
let res;
const info = await docsCoServer.getCallback(ctx, cmd.getDocId(), cmd.getUserIndex());
// info.wopiParams is null if it is not wopi
if (info?.wopiParams) {
const suggestedTargetType = `.${formatChecker.getStringFromFormat(cmd.getOutputFormat())}`;
const suggestedExt = `.${formatChecker.getStringFromFormat(cmd.getOutputFormat())}`;
const suggestedTarget = cmd.getSaveAsPath();
const storageFilePath = `${cmd.getSaveKey()}/${cmd.getOutputPath()}`;
const stream = await storage.createReadStream(ctx, storageFilePath);
const { wopiSrc, access_token } = info.wopiParams.userAuth;
await wopiClient.putRelativeFile(ctx, wopiSrc, access_token, null, stream.readStream, stream.contentLength, suggestedTargetType, false);
res = await wopiClient.putRelativeFile(ctx, wopiSrc, access_token, null, stream.readStream, stream.contentLength, suggestedExt, suggestedTarget, false);
}
return res;
}
exports.receiveTask = function(data, ack) {
return co(function* () {
Expand All @@ -1820,9 +1823,13 @@ exports.receiveTask = function(data, ack) {
yield getOutputData(ctx, cmd, outputData, cmd.getDocId(), null, additionalOutput);
} else if ('save' === command || 'savefromorigin' === command) {
let status = yield getOutputData(ctx, cmd, outputData, cmd.getSaveKey(), null, additionalOutput);
if (commonDefines.FileStatus.Ok === status && cmd.getIsSaveAs()) {
yield processWopiSaveAs(ctx, cmd);
if (commonDefines.FileStatus.Ok === status && (undefined !== cmd.getSaveAsPath() || cmd.getIsSaveAs)) {
//todo in case of wopi no need to send url. send it to avoid stubs in sdk
let saveAsRes = yield processWopiSaveAs(ctx, cmd);
if (!saveAsRes) {
outputData.setStatus('err');
outputData.setData(constants.VKEY);
}
}
} else if ('sfcm' === command) {
yield commandSfcCallback(ctx, cmd, true);
Expand Down
7 changes: 3 additions & 4 deletions DocService/sources/converterservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,9 @@ function getConverterHtmlHandler(req, res) {

let metadata = yield storage.headObject(ctx, fileTo);
let streamObj = yield storage.createReadStream(ctx, fileTo);
let postRes = yield wopiClient.putRelativeFile(ctx, wopiSrc, access_token, null, streamObj.readStream, metadata.ContentLength, `.${targetext}`, true);
if (postRes) {
let fileInfo = JSON.parse(postRes.body);
status.setUrl(fileInfo.HostEditUrl);
let putRelativeRes = yield wopiClient.putRelativeFile(ctx, wopiSrc, access_token, null, streamObj.readStream, metadata.ContentLength, `.${targetext}`, undefined, true);
if (putRelativeRes) {
status.setUrl(putRelativeRes.HostEditUrl);
status.setExtName('.' + targetext);
} else {
status.err = constants.UNKNOWN;
Expand Down
15 changes: 8 additions & 7 deletions DocService/sources/wopiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,36 +731,37 @@ function putFile(ctx, wopiParams, data, dataStream, dataSize, userLastChangeId,
return postRes;
});
}
function putRelativeFile(ctx, wopiSrc, access_token, data, dataStream, dataSize, suggestedTarget, isFileConversion) {
function putRelativeFile(ctx, wopiSrc, access_token, data, dataStream, dataSize, suggestedExt, suggestedTarget, isFileConversion) {
return co(function* () {
let postRes = null;
let res = undefined;
try {
ctx.logger.info('wopi putRelativeFile start');
const tenCallbackRequestTimeout = ctx.getCfg('services.CoAuthoring.server.callbackRequestTimeout', cfgCallbackRequestTimeout);

let uri = `${wopiSrc}?access_token=${access_token}`;
let filterStatus = yield checkIpFilter(ctx, uri);
if (0 !== filterStatus) {
return postRes;
return res;
}

let headers = {'X-WOPI-Override': 'PUT_RELATIVE', 'X-WOPI-SuggestedTarget': utf7.encode(suggestedTarget)};
let headers = {'X-WOPI-Override': 'PUT_RELATIVE', 'X-WOPI-SuggestedTarget': utf7.encode(suggestedTarget || suggestedExt)};
if (isFileConversion) {
headers['X-WOPI-FileConversion'] = isFileConversion;
}
yield fillStandardHeaders(ctx, headers, uri, access_token);
headers['Content-Type'] = mime.getType(suggestedTarget);
headers['Content-Type'] = mime.getType(suggestedExt);

ctx.logger.debug('wopi putRelativeFile request uri=%s headers=%j', uri, headers);
postRes = yield utils.postRequestPromise(ctx, uri, data, dataStream, dataSize, tenCallbackRequestTimeout, undefined, headers);
let postRes = yield utils.postRequestPromise(ctx, uri, data, dataStream, dataSize, tenCallbackRequestTimeout, undefined, headers);
ctx.logger.debug('wopi putRelativeFile response headers=%j', postRes.response.headers);
ctx.logger.debug('wopi putRelativeFile response body:%s', postRes.body);
res = JSON.parse(postRes.body);
} catch (err) {
ctx.logger.error('wopi error putRelativeFile:%s', err.stack);
} finally {
ctx.logger.info('wopi putRelativeFile end');
}
return postRes;
return res;
});
}
function renameFile(ctx, wopiParams, name) {
Expand Down

0 comments on commit c4022a0

Please sign in to comment.