Skip to content

Instantly share code, notes, and snippets.

@DivS-15
Last active August 17, 2022 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DivS-15/2fae7f0347729fb752e0232ee0cdaa4f to your computer and use it in GitHub Desktop.
Save DivS-15/2fae7f0347729fb752e0232ee0cdaa4f to your computer and use it in GitHub Desktop.
@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