Skip to content

Commit

Permalink
Fix: Fix shared links previewed in other subdomains (#173)
Browse files Browse the repository at this point in the history
* Fix: Fix shared links previewed in other subdomains

* Fix: Responding to comments
  • Loading branch information
Jeremy Press authored Jun 20, 2017
1 parent 28ec823 commit ab04be5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
23 changes: 14 additions & 9 deletions src/lib/viewers/office/OfficeViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,30 @@ const MESSAGE_HOST_READY = 'Host_PostmessageReady';
*/
setupRunmodeURL(appHost, fileId, sharedLink) {
// @TODO(jpress): Combine with setupWopiSrc Logic when removing the platform fork
let src = `${appHost}/integrations/officeonline/openExcelOnlinePreviewer`;
let route = '/integrations/officeonline/openExcelOnlinePreviewer';

const domain = document.createElement('a');
domain.href = sharedLink;

if (sharedLink) {
// Use the domain in case previewer has a different subdomain
// IE 11 does not support link.origin, so we combine the protocol and the hostname
const sharedLinkOrigin = `${domain.protocol}//${domain.hostname}`;
route = `${sharedLinkOrigin}${route}`;
// Find the shared or vanity name
const sharedName = sharedLink.split('/s/')[1];
if (sharedName) {
src = `${src}?s=${sharedName}&fileId=${fileId}`;
route = `${route}?s=${sharedName}&fileId=${fileId}`;
} else {
const tempAnchor = document.createElement('a');
tempAnchor.href = sharedLink;
const vanitySubdomain = tempAnchor.hostname.split('.')[0];
const vanityName = tempAnchor.href.split('/v/')[1];
src = `${src}?v=${vanityName}&vanity_subdomain=${vanitySubdomain}&fileId=${fileId}`;
const vanitySubdomain = domain.hostname.split('.')[0];
const vanityName = domain.href.split('/v/')[1];
route = `${route}?v=${vanityName}&vanity_subdomain=${vanitySubdomain}&fileId=${fileId}`;
}
} else {
src = `${src}?fileId=${fileId}`;
route = `${appHost}${route}?fileId=${fileId}`;
}

return src;
return route;
}

/**
Expand Down
41 changes: 33 additions & 8 deletions src/lib/viewers/office/__tests__/OfficeViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('lib/viewers/office/OfficeViewer', () => {
location: {
locale: 'en-US'
},
appHost: 'app.box.com',
apiHost: 'app.box.com',
appHost: 'https://app.box.com',
apiHost: 'https://app.box.com',
token: 'token'
});
stubs = {
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('lib/viewers/office/OfficeViewer', () => {
office.options.file.id,
office.options.sharedLink
);
expect(src).to.equal('app.box.com/integrations/officeonline/openExcelOnlinePreviewer?fileId=123');
expect(src).to.equal('https://app.box.com/integrations/officeonline/openExcelOnlinePreviewer?fileId=123');
});

it('should load a xlsx file and set the shared name in src url on load event when the file is a shared link', () => {
Expand All @@ -195,7 +195,9 @@ describe('lib/viewers/office/OfficeViewer', () => {
office.options.file.id,
office.options.sharedLink
);
expect(src).to.equal('app.box.com/integrations/officeonline/openExcelOnlinePreviewer?s=abcd&fileId=123');
expect(src).to.equal(
'https://app.box.com/integrations/officeonline/openExcelOnlinePreviewer?s=abcd&fileId=123'
);
});

it('should load a xlsx file and set the vanity name in src url on load event when the file is a vanity url without a subdomain', () => {
Expand All @@ -206,7 +208,7 @@ describe('lib/viewers/office/OfficeViewer', () => {
office.options.sharedLink
);
expect(src).to.equal(
'app.box.com/integrations/officeonline/openExcelOnlinePreviewer?v=test&vanity_subdomain=app&fileId=123'
'https://app.box.com/integrations/officeonline/openExcelOnlinePreviewer?v=test&vanity_subdomain=app&fileId=123'
);
});

Expand All @@ -218,21 +220,44 @@ describe('lib/viewers/office/OfficeViewer', () => {
office.options.sharedLink
);
expect(src).to.equal(
'app.box.com/integrations/officeonline/openExcelOnlinePreviewer?v=test&vanity_subdomain=cloud&fileId=123'
'https://cloud.app.box.com/integrations/officeonline/openExcelOnlinePreviewer?v=test&vanity_subdomain=cloud&fileId=123'
);
});

it('should load a xlsx file with a runmode sourced to the original domain if the appHost differs', () => {
office.options.sharedLink = 'https://app.box.com/v/test';
office.options.appHost = 'https://cloud.app.box.com';
let src = office.setupRunmodeURL(office.options.appHost, office.options.file.id, office.options.sharedLink);
expect(src).to.equal(
'https://app.box.com/integrations/officeonline/openExcelOnlinePreviewer?v=test&vanity_subdomain=app&fileId=123'
);

office.options.sharedLink = 'https://ibm.box.com/s/abcd';
src = office.setupRunmodeURL(office.options.appHost, office.options.file.id, office.options.sharedLink);
expect(src).to.equal(
'https://ibm.box.com/integrations/officeonline/openExcelOnlinePreviewer?s=abcd&fileId=123'
);

office.options.sharedLink = 'https://cloud.box.com/s/abcd';
office.options.appHost = 'https://app.app.box.com';

src = office.setupRunmodeURL(office.options.appHost, office.options.file.id, office.options.sharedLink);
expect(src).to.equal(
'https://cloud.box.com/integrations/officeonline/openExcelOnlinePreviewer?s=abcd&fileId=123'
);
});
});

describe('setupWOPISrc()', () => {
it('should append the file ID if there is no shared link', () => {
const src = office.setupWOPISrc(office.options.apiHost, office.options.file.id, office.options.sharedLink);
expect(src).to.equal('app.box.com/wopi/files/123');
expect(src).to.equal('https://app.box.com/wopi/files/123');
});

it('should append the shared name and file ID if there is a shared link', () => {
office.options.sharedLink = 'https://app.box.com/s/abcd';
const src = office.setupWOPISrc(office.options.apiHost, office.options.file.id, office.options.sharedLink);
expect(src).to.equal('app.box.com/wopi/files/s_abcd_f_123');
expect(src).to.equal('https://app.box.com/wopi/files/s_abcd_f_123');
});

it('should not append the shared name if there is a vanity link', () => {
Expand Down

0 comments on commit ab04be5

Please sign in to comment.