Skip to content

Commit 2cb26e7

Browse files
Fixing file upload issue with android
1 parent 5bff8a5 commit 2cb26e7

File tree

3 files changed

+172
-18
lines changed

3 files changed

+172
-18
lines changed
Lines changed: 166 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,170 @@
1-
/**
2-
* Metro configuration for React Native
3-
* https://github.com/facebook/react-native
4-
*
5-
* @format
6-
*/
1+
/* eslint-env node */
2+
function resolvePath(...parts) {
3+
const thisPath = PATH.resolve.apply(PATH, parts);
4+
if (!FS.existsSync(thisPath)) return;
5+
6+
return FS.realpathSync(thisPath);
7+
}
8+
9+
function isExternalModule(modulePath) {
10+
return modulePath.substring(0, __dirname.length) !== __dirname;
11+
}
12+
13+
function listDirectories(rootPath, cb) {
14+
FS.readdirSync(rootPath).forEach(fileName => {
15+
if (fileName.charAt(0) === '.') return;
16+
17+
let fullFileName = PATH.join(rootPath, fileName),
18+
stats = FS.lstatSync(fullFileName),
19+
symbolic = false;
20+
21+
if (stats.isSymbolicLink()) {
22+
fullFileName = resolvePath(fullFileName);
23+
if (!fullFileName) return;
24+
25+
stats = FS.lstatSync(fullFileName);
26+
27+
symbolic = true;
28+
}
29+
30+
if (!stats.isDirectory()) return;
31+
32+
const external = isExternalModule(fullFileName);
33+
cb({rootPath, symbolic, external, fullFileName, fileName});
34+
});
35+
}
36+
37+
function buildFullModuleMap(
38+
moduleRoot,
39+
mainModuleMap,
40+
externalModuleMap,
41+
_alreadyVisited,
42+
_prefix,
43+
) {
44+
if (!moduleRoot) return;
45+
46+
const alreadyVisited = _alreadyVisited || {},
47+
prefix = _prefix;
48+
49+
if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return;
50+
51+
alreadyVisited[moduleRoot] = true;
52+
53+
listDirectories(
54+
moduleRoot,
55+
({fileName, fullFileName, symbolic, external}) => {
56+
if (symbolic)
57+
return buildFullModuleMap(
58+
resolvePath(fullFileName, 'node_modules'),
59+
mainModuleMap,
60+
externalModuleMap,
61+
alreadyVisited,
62+
);
63+
64+
const moduleMap = external ? externalModuleMap : mainModuleMap,
65+
moduleName = prefix ? PATH.join(prefix, fileName) : fileName;
66+
67+
if (fileName.charAt(0) !== '@') moduleMap[moduleName] = fullFileName;
68+
else
69+
return buildFullModuleMap(
70+
fullFileName,
71+
mainModuleMap,
72+
externalModuleMap,
73+
alreadyVisited,
74+
fileName,
75+
);
76+
},
77+
);
78+
}
79+
80+
function buildModuleResolutionMap() {
81+
const moduleMap = {},
82+
externalModuleMap = {};
83+
84+
buildFullModuleMap(baseModulePath, moduleMap, externalModuleMap);
85+
86+
// Root project modules take precedence over external modules
87+
return Object.assign({}, externalModuleMap, moduleMap);
88+
}
89+
90+
function findAlernateRoots(
91+
moduleRoot = baseModulePath,
92+
alternateRoots = [],
93+
_alreadyVisited,
94+
) {
95+
const alreadyVisited = _alreadyVisited || {};
96+
if (alreadyVisited && alreadyVisited.hasOwnProperty(moduleRoot)) return;
97+
98+
alreadyVisited[moduleRoot] = true;
99+
100+
listDirectories(moduleRoot, ({fullFileName, fileName, external}) => {
101+
if (fileName.charAt(0) !== '@') {
102+
if (external) alternateRoots.push(fullFileName);
103+
} else {
104+
findAlernateRoots(fullFileName, alternateRoots, alreadyVisited);
105+
}
106+
});
107+
108+
return alternateRoots;
109+
}
110+
111+
function getPolyfillHelper() {
112+
let getPolyfills;
113+
114+
// Get default react-native polyfills
115+
try {
116+
getPolyfills = require('react-native/rn-get-polyfills');
117+
} catch (e) {
118+
getPolyfills = () => [];
119+
}
120+
121+
// See if project has custom polyfills, if so, include the PATH to them
122+
try {
123+
const customPolyfills = require.resolve('./polyfills.js');
124+
getPolyfills = (function(originalGetPolyfills) {
125+
return () => originalGetPolyfills().concat(customPolyfills);
126+
})(getPolyfills);
127+
} catch (e) {
128+
//ignore
129+
}
130+
131+
return getPolyfills;
132+
}
133+
134+
const PATH = require('path');
135+
const FS = require('fs'),
136+
blacklist = require('metro-config/src/defaults/blacklist');
137+
138+
const repoDir = PATH.dirname(PATH.dirname(__dirname));
139+
140+
const moduleBlacklist = [
141+
new RegExp(repoDir + '/examples/ExpoMessaging/.*'),
142+
// new RegExp(repoDir + '/native-example/(.*)'),
143+
new RegExp(repoDir + '/expo-package/.*'),
144+
new RegExp(repoDir + '/native-package/node_modules/.*'),
145+
new RegExp(repoDir + '/node_modules/.*'),
146+
],
147+
baseModulePath = resolvePath(__dirname, 'node_modules'),
148+
// watch alternate roots (outside of project root)
149+
alternateRoots = findAlernateRoots(),
150+
// build full module map for proper
151+
// resolution of modules in external roots
152+
extraNodeModules = buildModuleResolutionMap();
153+
154+
if (alternateRoots && alternateRoots.length)
155+
console.log('Found alternate project roots: ', alternateRoots);
7156

8157
module.exports = {
9-
transformer: {
10-
getTransformOptions: async () => ({
11-
transform: {
12-
experimentalImportSupport: false,
13-
inlineRequires: false,
14-
},
15-
}),
158+
resolver: {
159+
blacklistRE: blacklist(moduleBlacklist),
160+
extraNodeModules,
161+
useWatchman: false,
162+
},
163+
watchFolders: [PATH.resolve(__dirname)].concat(alternateRoots),
164+
// transformer: {
165+
// babelTransformerPath: require.resolve('./compiler/transformer'),
166+
// },
167+
serializer: {
168+
getPolyfills: getPolyfillHelper(),
16169
},
17170
};

native-package/src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ registerNativeHandlers({
6464
const res = await DocumentPicker.pick({
6565
type: [DocumentPicker.types.allFiles],
6666
});
67-
let { uri } = res;
68-
if (Platform.OS === 'android') {
69-
uri = 'file://' + res.path;
70-
}
67+
const { uri } = res;
7168

7269
return {
7370
cancelled: false,

src/components/MessageInput.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,11 @@ class MessageInput extends PureComponent {
558558
this.props.channel,
559559
);
560560
} else {
561-
response = await this.props.channel.sendFile(file.uri);
561+
response = await this.props.channel.sendFile(
562+
file.uri,
563+
file.name,
564+
file.type,
565+
);
562566
}
563567
} catch (e) {
564568
console.warn(e);

0 commit comments

Comments
 (0)