-
-
Save DivS-15/2fae7f0347729fb752e0232ee0cdaa4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltWorker | |
class UploadWorker @AssistedInject constructor( | |
@Assisted ctx: Context, | |
@Assisted workerParameters: WorkerParameters, | |
private val ioDispatcher: CoroutineDispatcher | |
) : CoroutineWorker(ctx, workerParameters) { | |
private val storageReference = Firebase.storage.reference.child("user_images") | |
override suspend fun doWork(): Result = withContext(ioDispatcher) { | |
val inputFileUri = inputData.getString(KEY_IMAGE_URI) | |
return@withContext try { | |
uploadImageFromUri(Uri.parse(inputFileUri)) | |
} catch (exception: Exception) { | |
exception.printStackTrace() | |
Result.failure() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
Result.failure() | |
} | |
} | |
private fun uploadImageFromUri(fileUri: Uri): Result { | |
fileUri.lastPathSegment?.let { | |
val photoRef = storageReference.child(it) | |
Log.d("Upload Worker", it) | |
photoRef.putFile(fileUri) | |
} | |
return Result.success() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment