Skip to content

Commit

Permalink
Fix getDownloadURL to properly append the xsrf token without a slash.
Browse files Browse the repository at this point in the history
Before, we would get URLs like http://example.com/files/file.txt/?_xsrf=token, and now the slash after file.txt is eliminated.
  • Loading branch information
jasongrout committed Aug 15, 2019
1 parent 860877e commit 9043574
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/attachments/src/model.ts
Expand Up @@ -374,6 +374,9 @@ export class AttachmentsResolver implements IRenderMime.IResolver {

/**
* Get the download url of a given absolute server path.
*
* #### Notes
* The returned URL may include a query parameter.
*/
getDownloadUrl(path: string): Promise<string> {
if (this._parent && !path.startsWith('attachment:')) {
Expand Down
7 changes: 2 additions & 5 deletions packages/filebrowser/src/model.ts
Expand Up @@ -324,11 +324,8 @@ export class FileBrowserModel implements IDisposable {
const url = await this.manager.services.contents.getDownloadUrl(path);
let element = document.createElement('a');
document.body.appendChild(element);
element.setAttribute('href', url);
// Chrome doesn't get the right name automatically
const parts = path.split('/');
const name = parts[parts.length - 1];
element.setAttribute('download', name);
element.href = url;
element.download = '';
element.click();
document.body.removeChild(element);
return void 0;
Expand Down
3 changes: 3 additions & 0 deletions packages/rendermime-interfaces/src/index.ts
Expand Up @@ -340,6 +340,9 @@ export namespace IRenderMime {

/**
* Get the download url for a given absolute url path.
*
* #### Notes
* This URL may include a query parameter.
*/
getDownloadUrl(url: string): Promise<string>;

Expand Down
3 changes: 3 additions & 0 deletions packages/rendermime/src/registry.ts
Expand Up @@ -328,6 +328,9 @@ export namespace RenderMimeRegistry {

/**
* Get the download url of a given absolute url path.
*
* #### Notes
* This URL may include a query parameter.
*/
getDownloadUrl(url: string): Promise<string> {
if (this.isLocal(url)) {
Expand Down
14 changes: 13 additions & 1 deletion packages/services/src/contents/index.ts
Expand Up @@ -276,6 +276,9 @@ export namespace Contents {
*
* @param A promise which resolves with the absolute POSIX
* file path on the server.
*
* #### Notes
* The returned URL may include a query parameter.
*/
getDownloadUrl(path: string): Promise<string>;

Expand Down Expand Up @@ -420,6 +423,9 @@ export namespace Contents {
*
* @param A promise which resolves with the absolute POSIX
* file path on the server.
*
* #### Notes
* The returned URL may include a query parameter.
*/
getDownloadUrl(localPath: string): Promise<string>;

Expand Down Expand Up @@ -690,6 +696,8 @@ export class ContentsManager implements Contents.IManager {
*
* #### Notes
* It is expected that the path contains no relative paths.
*
* The returned URL may include a query parameter.
*/
getDownloadUrl(path: string): Promise<string> {
let [drive, localPath] = this._driveForPath(path);
Expand Down Expand Up @@ -1041,13 +1049,17 @@ export class Drive implements Contents.IDrive {
*
* #### Notes
* It is expected that the path contains no relative paths.
*
* The returned URL may include a query parameter.
*/
getDownloadUrl(localPath: string): Promise<string> {
let baseUrl = this.serverSettings.baseUrl;
let url = URLExt.join(baseUrl, FILES_URL, URLExt.encodeParts(localPath));
const xsrfTokenMatch = document.cookie.match('\\b_xsrf=([^;]*)\\b');
if (xsrfTokenMatch) {
url = URLExt.join(url, `?_xsrf=${xsrfTokenMatch[1]}`);
const fullurl = new URL(url);
fullurl.searchParams.append('_xsrf', xsrfTokenMatch[1]);
url = fullurl.toString();
}
return Promise.resolve(url);
}
Expand Down

0 comments on commit 9043574

Please sign in to comment.