[MM-21888] Don't use localPath when it's the share extension cache dir (#3845)

* Don't use localPath when it's the share extension cache dir

* Move android pasted images to cache image folder

* Use Files.move instead of FileInput / FileOutput stream

* Remove commented code and not needed imports
This commit is contained in:
Miguel Alatzar 2020-01-27 09:58:30 -07:00 committed by Amit Uttam
parent 7e7c652828
commit d25561bb83
5 changed files with 30 additions and 3 deletions

View file

@ -212,7 +212,7 @@ public class MainApplication extends NavigationApplication implements INotificat
instance = this;
// Delete any previous temp files created by the app
File tempFolder = new File(getApplicationContext().getCacheDir(), "mmShare");
File tempFolder = new File(getApplicationContext().getCacheDir(), ShareModule.CACHE_DIR_NAME);
RealPathUtil.deleteTempFiles(tempFolder);
Log.i("ReactNative", "Cleaning temp cache " + tempFolder.getAbsolutePath());

View file

@ -16,8 +16,11 @@ import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.mattermost.share.RealPathUtil;
import com.mattermost.share.ShareModule;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
public class RNPasteableEditTextOnPasteListener implements RNEditTextOnPasteListener {
@ -83,6 +86,14 @@ public class RNPasteableEditTextOnPasteListener implements RNEditTextOnPasteList
// Get fileName
String fileName = URLUtil.guessFileName(uri, null, mimeType);
if (uri.contains(ShareModule.CACHE_DIR_NAME)) {
uri = moveToImagesCache(uri, fileName);
}
if (uri == null) {
return;
}
// Get fileSize
long fileSize;
try {
@ -119,4 +130,17 @@ public class RNPasteableEditTextOnPasteListener implements RNEditTextOnPasteList
event
);
}
private String moveToImagesCache(String src, String fileName) {
ReactContext ctx = (ReactContext)mEditText.getContext();
String dest = ctx.getCacheDir().getAbsolutePath() + "/Images/" + fileName;
try {
Files.move(Paths.get(src), Paths.get(dest));
} catch (Exception err) {
return null;
}
return dest;
}
}

View file

@ -113,7 +113,7 @@ public class RealPathUtil {
}
File cacheDir = new File(context.getCacheDir(), "mmShare");
File cacheDir = new File(context.getCacheDir(), ShareModule.CACHE_DIR_NAME);
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}

View file

@ -39,6 +39,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
private final OkHttpClient client = new OkHttpClient();
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private final MainApplication mApplication;
public static final String CACHE_DIR_NAME = "mmShare";
public ShareModule(MainApplication application, ReactApplicationContext reactContext) {
super(reactContext);
@ -67,6 +68,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
@Override
public Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<>(1);
constants.put("cacheDirName", CACHE_DIR_NAME);
constants.put("isOpened", mApplication.sharedExtensionIsOpened);
mApplication.sharedExtensionIsOpened = false;
return constants;
@ -133,7 +135,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
Activity currentActivity = getCurrentActivity();
if (currentActivity != null) {
this.tempFolder = new File(currentActivity.getCacheDir(), "mmShare");
this.tempFolder = new File(currentActivity.getCacheDir(), CACHE_DIR_NAME);
Intent intent = currentActivity.getIntent();
action = intent.getAction();
type = intent.getType();

View file

@ -50,6 +50,7 @@ jest.doMock('react-native', () => {
},
MattermostShare: {
close: jest.fn(),
cacheDirName: 'mmShare',
},
PlatformConstants: {
forceTouchAvailable: false,