Skip to content

Commit

Permalink
Add saveFileToDownloads method
Browse files Browse the repository at this point in the history
  • Loading branch information
PixoDev committed Jul 3, 2023
1 parent e96e4ce commit 42face1
Show file tree
Hide file tree
Showing 18 changed files with 5,867 additions and 3,841 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencies {
//noinspection GradleDynamicVersion
implementation("androidx.work:work-runtime:$work_version")
implementation("androidx.work:work-runtime-ktx:$work_version")
implementation 'com.facebook.react:react-native:0.70.0'
implementation 'com.facebook.react:react-native:+'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.madgag.spongycastle:bcpkix-jdk15on:$spongycastle_version"
implementation "com.madgag.spongycastle:bcpg-jdk15on:$spongycastle_version"
Expand Down
13 changes: 12 additions & 1 deletion android/src/main/java/com/internxt/mobilesdk/MobileSdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,21 @@ class MobileSdkModule(val reactContext: ReactApplicationContext) :

} catch (e: Exception) {
e.printStackTrace()
// To late to reject, we only enqueue the job
// Too late to reject, we only enqueue the job
}
}

@ReactMethod
fun saveToDownloads(originUri: String, promise: Promise) {
try {
FS.saveFileToDownloadsDirectory(reactApplicationContext, originUri)
} catch (exception: Exception) {
exception.printStackTrace()
promise.reject(exception)
}

}


companion object {
const val NAME = "MobileSdk"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.internxt.mobilesdk.services
import android.content.ContentResolver
import android.content.ContentValues
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.webkit.MimeTypeMap
import com.facebook.react.bridge.ReactApplicationContext
import com.internxt.mobilesdk.utils.FileAccessRejectionException
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException
import java.io.*
import com.internxt.mobilesdk.utils.Logger
import kotlin.io.path.Path


// This class is called FS to avoid naming conflicts with Java FileSystem class
object FS {
Expand Down Expand Up @@ -84,4 +91,61 @@ object FS {
return true
}
}


@Throws(NoSuchFileException::class, FileAlreadyExistsException::class, IOException::class)
fun saveFileToDownloadsDirectory(context: ReactApplicationContext, originalFilePath: String) {
val filename = originalFilePath.substring(originalFilePath.lastIndexOf(File.separator));
val fileInputStream: InputStream =
context.contentResolver.openInputStream(getFileUri(originalFilePath, true))
?: throw Exception("Cannot open Input stream at path $originalFilePath")


if (false && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

val contentValues = ContentValues();

contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
val mimeType = getMimeType(context.contentResolver, originalFilePath)
Logger.info("Mime type")
if (mimeType != null) {
Logger.info(mimeType)
} else {
Logger.info("No mime")
}
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
?: throw Exception("FileUri not inserted in content resolver");

Logger.info("File inserted in content database")
}

val buffer = ByteArray(8 * 1024) // 8KB buffer
var bytesRead: Int

val downloadsDir =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()

val destination = File(downloadsDir, filename)
val outputStream = FileOutputStream(destination)

try {
while (fileInputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
}
} catch (e: IOException) {
e.printStackTrace()
} finally {
fileInputStream.close()
outputStream.close()
}

Logger.info("Closed stream")

}

fun getMimeType(contentResolver: ContentResolver, uri: String): String? {
return contentResolver.getType(getFileUri(uri, true))
}
}
1 change: 1 addition & 0 deletions example/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@tailwind utilities
6 changes: 4 additions & 2 deletions example/ios/MobileSdkExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -598,6 +598,7 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
};
name = Debug;
Expand Down Expand Up @@ -635,7 +636,7 @@
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -661,6 +662,7 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
};
Expand Down
10 changes: 10 additions & 0 deletions example/ios/MobileSdkExample.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 42face1

Please sign in to comment.