/* @flow */
import { CameraRoll, Platform } from 'react-native';
import RNFetchBlob from 'rn-fetch-blob';

import type { Auth } from './apiTypes';
import { getAuthHeader, getFullUrl } from '../utils/url';
import userAgent from '../utils/userAgent';

export default (src: string, auth: Auth) => {
  const fullUrlWithAPIKey = `${getFullUrl(src, auth.realm)}?api_key=${auth.apiKey}`;

  if (Platform.OS === 'ios') {
    return CameraRoll.saveToCameraRoll(fullUrlWithAPIKey);
  }
  return RNFetchBlob.config({
    addAndroidDownloads: {
      path: `${RNFetchBlob.fs.dirs.DownloadDir}/${src.split('/').pop()}`,
      useDownloadManager: true,
      mime: 'text/plain', // Android DownloadManager fails if the url is missing a file extension
      title: src.split('/').pop(),
      notification: true,
    },
  }).fetch('GET', getFullUrl(src, auth.realm), {
    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
    'User-Agent': userAgent,
    Authorization: getAuthHeader(auth.email, auth.apiKey),
  });
};

@@ -301,31 +301,22 @@
 nst 
-fullUrlWithAPIKey = %60$%7B
+absoluteUrl = 
 getF
@@ -342,33 +342,8 @@
 alm)
-%7D?api_key=$%7Bauth.apiKey%7D%60
 ;%0A%0A 
@@ -372,16 +372,155 @@
 ios') %7B%0A
+    const delimiter = absoluteUrl.includes('?') ? '&' : '?';%0A    const urlWithApiKey = %60$%7BabsoluteUrl%7D$%7Bdelimiter%7Dapi_key=$%7Bauth.apiKey%7D%60;%0A
     retu
@@ -554,22 +554,18 @@
 oll(
-fullU
+u
 rlWithA
-PI
+pi
 Key)
@@ -921,35 +921,19 @@
 T', 
-getFullUrl(src, auth.realm)
+absoluteUrl
 , %7B%0A

Tweak URL-with-key logic to be a bit more defensive.