diff --git a/keyserver/package.json b/keyserver/package.json --- a/keyserver/package.json +++ b/keyserver/package.json @@ -35,6 +35,7 @@ "concurrently": "^5.3.0", "flow-bin": "^0.182.0", "flow-typed": "^3.2.1", + "internal-ip": "4.3.0", "jest": "^26.6.3", "nodemon": "^2.0.4" }, diff --git a/keyserver/src/creators/upload-creator.js b/keyserver/src/creators/upload-creator.js --- a/keyserver/src/creators/upload-creator.js +++ b/keyserver/src/creators/upload-creator.js @@ -2,7 +2,6 @@ import crypto from 'crypto'; -import { shimUploadURI } from 'lib/media/media-utils'; import type { MediaType, UploadMultimediaResult, @@ -39,7 +38,7 @@ return { uploadResult: { id, - uri: shimUploadURI(getUploadURL(id, secret), viewer.platformDetails), + uri: getUploadURL(id, secret), dimensions, mediaType, loop, diff --git a/keyserver/src/fetchers/upload-fetchers.js b/keyserver/src/fetchers/upload-fetchers.js --- a/keyserver/src/fetchers/upload-fetchers.js +++ b/keyserver/src/fetchers/upload-fetchers.js @@ -1,11 +1,13 @@ // @flow +import ip from 'internal-ip'; import _keyBy from 'lodash/fp/keyBy'; import type { Media } from 'lib/types/media-types'; import type { MediaMessageServerDBContent } from 'lib/types/messages/media.js'; import { getUploadIDsFromMediaMessageServerDBContents } from 'lib/types/messages/media.js'; import type { ThreadFetchMediaResult } from 'lib/types/thread-types'; +import { isDev } from 'lib/utils/dev-utils'; import { ServerError } from 'lib/utils/errors'; import { dbQuery, SQL } from '../database/database'; @@ -81,7 +83,13 @@ function getUploadURL(id: string, secret: string): string { const { baseDomain, basePath } = getAndAssertCommAppURLFacts(); - return `${baseDomain}${basePath}upload/${id}/${secret}`; + const uploadPath = `${basePath}upload/${id}/${secret}`; + if (isDev) { + const ipV4 = ip.v4.sync() || 'localhost'; + const port = parseInt(process.env.PORT, 10) || 3000; + return `http://${ipV4}:${port}${uploadPath}`; + } + return `${baseDomain}${uploadPath}`; } function mediaFromRow(row: Object): Media { diff --git a/lib/media/media-utils.js b/lib/media/media-utils.js --- a/lib/media/media-utils.js +++ b/lib/media/media-utils.js @@ -2,7 +2,6 @@ import invariant from 'invariant'; -import type { PlatformDetails } from '../types/device-types'; import type { Media } from '../types/media-types'; import type { MultimediaMessageInfo, @@ -11,15 +10,6 @@ const maxDimensions = Object.freeze({ width: 1920, height: 1920 }); -const localhostRegex = /^http:\/\/localhost/; -function shimUploadURI(uri: string, platformDetails: ?PlatformDetails): string { - if (!platformDetails || platformDetails.platform !== 'android') { - return uri; - } - // We do this for testing in the Android emulator - return uri.replace(localhostRegex, 'http://10.0.2.2'); -} - function contentStringForMediaArray(media: $ReadOnlyArray): string { if (media.length === 0) { return 'corrupted media'; @@ -64,7 +54,6 @@ export { maxDimensions, - shimUploadURI, contentStringForMediaArray, multimediaMessagePreview, isLocalUploadID, diff --git a/lib/shared/messages/multimedia-message-spec.js b/lib/shared/messages/multimedia-message-spec.js --- a/lib/shared/messages/multimedia-message-spec.js +++ b/lib/shared/messages/multimedia-message-spec.js @@ -5,10 +5,8 @@ import { contentStringForMediaArray, multimediaMessagePreview, - shimUploadURI, } from '../../media/media-utils'; import type { PlatformDetails } from '../../types/device-types'; -import type { Media, Video, Image } from '../../types/media-types'; import { messageTypes, assertMessageType, @@ -17,7 +15,6 @@ import type { MessageInfo, RawMessageInfo, - RawMultimediaMessageInfo, ClientDBMessageInfo, } from '../../types/message-types'; import type { @@ -202,32 +199,22 @@ platformDetails: ?PlatformDetails, ): RawMediaMessageInfo | RawImagesMessageInfo | RawUnsupportedMessageInfo { if (rawMessageInfo.type === messageTypes.IMAGES) { - const shimmedRawMessageInfo = shimMediaMessageInfo( - rawMessageInfo, - platformDetails, - ); - return shimmedRawMessageInfo; - } else { - const shimmedRawMessageInfo = shimMediaMessageInfo( - rawMessageInfo, - platformDetails, - ); - // TODO figure out first native codeVersion supporting video playback - if (hasMinCodeVersion(platformDetails, 158)) { - return shimmedRawMessageInfo; - } - const { id } = shimmedRawMessageInfo; - invariant(id !== null && id !== undefined, 'id should be set on server'); - return { - type: messageTypes.UNSUPPORTED, - id, - threadID: shimmedRawMessageInfo.threadID, - creatorID: shimmedRawMessageInfo.creatorID, - time: shimmedRawMessageInfo.time, - robotext: multimediaMessagePreview(shimmedRawMessageInfo), - unsupportedMessageInfo: shimmedRawMessageInfo, - }; + return rawMessageInfo; } + if (hasMinCodeVersion(platformDetails, 158)) { + return rawMessageInfo; + } + const { id } = rawMessageInfo; + invariant(id !== null && id !== undefined, 'id should be set on server'); + return { + type: messageTypes.UNSUPPORTED, + id, + threadID: rawMessageInfo.threadID, + creatorID: rawMessageInfo.creatorID, + time: rawMessageInfo.time, + robotext: multimediaMessagePreview(rawMessageInfo), + unsupportedMessageInfo: rawMessageInfo, + }; }, unshimMessageInfo( @@ -313,54 +300,6 @@ includedInRepliesCount: true, }); -function shimMediaMessageInfo( - rawMessageInfo: RawMultimediaMessageInfo, - platformDetails: ?PlatformDetails, -): RawMultimediaMessageInfo { - if (rawMessageInfo.type === messageTypes.IMAGES) { - let uriChanged = false; - const newMedia: Image[] = []; - for (const singleMedia of rawMessageInfo.media) { - const shimmedURI = shimUploadURI(singleMedia.uri, platformDetails); - if (shimmedURI === singleMedia.uri) { - newMedia.push(singleMedia); - } else { - newMedia.push(({ ...singleMedia, uri: shimmedURI }: Image)); - uriChanged = true; - } - } - if (!uriChanged) { - return rawMessageInfo; - } - return ({ - ...rawMessageInfo, - media: newMedia, - }: RawImagesMessageInfo); - } else { - let uriChanged = false; - const newMedia: Media[] = []; - for (const singleMedia of rawMessageInfo.media) { - const shimmedURI = shimUploadURI(singleMedia.uri, platformDetails); - if (shimmedURI === singleMedia.uri) { - newMedia.push(singleMedia); - } else if (singleMedia.type === 'photo') { - newMedia.push(({ ...singleMedia, uri: shimmedURI }: Image)); - uriChanged = true; - } else { - newMedia.push(({ ...singleMedia, uri: shimmedURI }: Video)); - uriChanged = true; - } - } - if (!uriChanged) { - return rawMessageInfo; - } - return ({ - ...rawMessageInfo, - media: newMedia, - }: RawMediaMessageInfo); - } -} - // Four photos were uploaded before dimensions were calculated server-side, // and delivered to clients without dimensions in the MultimediaMessageInfo. const preDimensionUploads = {