From ae4b8cf0d333724a4ffb8e940d007f9e3b37786c Mon Sep 17 00:00:00 2001 From: Donovan Preston Date: Mon, 19 Nov 2018 15:10:57 -0500 Subject: [PATCH] Add setApiUrlPrefix and use it in the android version. --- android/android.js | 2 ++ android/app/src/main/AndroidManifest.xml | 1 + app/api.js | 33 +++++++++++++++++------- app/fileReceiver.js | 14 ++++++---- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/android/android.js b/android/android.js index 83e21c9c..10b1bf53 100644 --- a/android/android.js +++ b/android/android.js @@ -23,6 +23,7 @@ import choo from 'choo'; import html from 'choo/html'; import Raven from 'raven-js'; +import { setApiUrlPrefix } from '../app/api'; import assets from '../common/assets'; import Header from '../app/ui/header'; import locale from '../common/locales'; @@ -38,6 +39,7 @@ import error from './pages/error'; if (navigator.userAgent === 'Send Android') { assets.setPrefix('/android_asset'); + setApiUrlPrefix('https://send2.dev.lcip.org'); } const app = choo(); diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index e3086ea3..786540c6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ diff --git a/app/api.js b/app/api.js index ce4c8a16..49b38358 100644 --- a/app/api.js +++ b/app/api.js @@ -20,6 +20,15 @@ export function getFileProtocolWssUrl() { return fileProtocolWssUrl; } +let apiUrlPrefix = ''; +export function getApiUrl(path) { + return apiUrlPrefix + path; +} + +export function setApiUrlPrefix(prefix) { + apiUrlPrefix = prefix; +} + function post(obj, bearerToken) { const h = { 'Content-Type': 'application/json' @@ -62,13 +71,16 @@ async function fetchWithAuthAndRetry(url, params, keychain) { } export async function del(id, owner_token) { - const response = await fetch(`/api/delete/${id}`, post({ owner_token })); + const response = await fetch( + getApiUrl(`/api/delete/${id}`), + post({ owner_token }) + ); return response.ok; } export async function setParams(id, owner_token, bearerToken, params) { const response = await fetch( - `/api/params/${id}`, + getApiUrl(`/api/params/${id}`), post( { owner_token, @@ -81,7 +93,10 @@ export async function setParams(id, owner_token, bearerToken, params) { } export async function fileInfo(id, owner_token) { - const response = await fetch(`/api/info/${id}`, post({ owner_token })); + const response = await fetch( + getApiUrl(`/api/info/${id}`), + post({ owner_token }) + ); if (response.ok) { const obj = await response.json(); @@ -93,7 +108,7 @@ export async function fileInfo(id, owner_token) { export async function metadata(id, keychain) { const result = await fetchWithAuthAndRetry( - `/api/metadata/${id}`, + getApiUrl(`/api/metadata/${id}`), { method: 'GET' }, keychain ); @@ -115,7 +130,7 @@ export async function metadata(id, keychain) { export async function setPassword(id, owner_token, keychain) { const auth = await keychain.authKeyB64(); const response = await fetch( - `/api/password/${id}`, + getApiUrl(`/api/password/${id}`), post({ owner_token, auth }) ); return response.ok; @@ -251,7 +266,7 @@ export function uploadWs( async function downloadS(id, keychain, signal) { const auth = await keychain.authHeader(); - const response = await fetch(`/api/download/${id}`, { + const response = await fetch(getApiUrl(`/api/download/${id}`), { signal: signal, method: 'GET', headers: { Authorization: auth } @@ -323,7 +338,7 @@ function download(id, keychain, onprogress, canceller) { } }); const auth = await keychain.authHeader(); - xhr.open('get', `/api/download/blob/${id}`); + xhr.open('get', getApiUrl(`/api/download/blob/${id}`)); xhr.setRequestHeader('Authorization', auth); xhr.responseType = 'blob'; xhr.send(); @@ -358,7 +373,7 @@ export function downloadFile(id, keychain, onprogress) { export async function getFileList(bearerToken) { const headers = new Headers({ Authorization: `Bearer ${bearerToken}` }); - const response = await fetch('/api/filelist', { headers }); + const response = await fetch(getApiUrl('/api/filelist'), { headers }); if (response.ok) { return response.body; // stream } @@ -367,7 +382,7 @@ export async function getFileList(bearerToken) { export async function setFileList(bearerToken, data) { const headers = new Headers({ Authorization: `Bearer ${bearerToken}` }); - const response = await fetch('/api/filelist', { + const response = await fetch(getApiUrl('/api/filelist'), { headers, method: 'POST', body: data diff --git a/app/fileReceiver.js b/app/fileReceiver.js index e2aa907d..58182999 100644 --- a/app/fileReceiver.js +++ b/app/fileReceiver.js @@ -1,7 +1,7 @@ import Nanobus from 'nanobus'; import Keychain from './keychain'; import { delay, bytes, streamToArrayBuffer } from './utils'; -import { downloadFile, metadata } from './api'; +import { downloadFile, metadata, getApiUrl } from './api'; import { blobStream } from './streams'; import Zip from './zip'; @@ -145,14 +145,18 @@ export default class FileReceiver extends Nanobus { onprogress(0); if (noSave) { - const res = await fetch(`/api/download/${this.fileInfo.id}`); + const res = await fetch(getApiUrl(`/api/download/${this.fileInfo.id}`)); if (res.status !== 200) { throw new Error(res.status); } } else { - const downloadUrl = `${location.protocol}//${ - location.host - }/api/download/${this.fileInfo.id}`; + const downloadPath = `/api/download/${this.fileInfo.id}`; + let downloadUrl = getApiUrl(downloadPath); + if (downloadUrl === downloadPath) { + downloadUrl = `${location.protocol}//${location.host}/api/download/${ + this.fileInfo.id + }`; + } const a = document.createElement('a'); a.href = downloadUrl; document.body.appendChild(a);