Skip to content

Commit

Permalink
Storage sdk: Implement update functionality in non gms.
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-hamel committed Jun 30, 2023
1 parent 56512e0 commit a20c178
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ internal class GmsFileRepositoryImpl(

override fun open() = Unit

override fun update() = Unit
override fun updateFile(localFileToUpload: File, fileId: String, parentId: String?) = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ internal class GmsFileRemoteDataSourceImpl(private val apiService: GoogleDriveAp
return response.toOmhFile()
}

override fun updateFile(localFileToUpload: File, fileId: String, parentId: String?) = null

private fun getStringMimeTypeFromLocalFile(file: File) = MimeTypeMap
.getSingleton()
.getMimeTypeFromExtension(file.extension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
Expand Down Expand Up @@ -73,4 +74,16 @@ internal interface GoogleStorageApiService {
@Path(FILE_ID) fileId: String,
@Query(QUERY_MIME_TYPE) mimeType: String
): Call<ResponseBody>

@PATCH("$UPLOAD_FILES_PARTICLE/{$FILE_ID}")
fun updateFile(
@Body filePart: RequestBody,
@Path(FILE_ID) fileId: String
): Call<FileRemoteResponse>

@PATCH("$FILES_PARTICLE/{$FILE_ID}")
fun updateMetaData(
@Body filePart: RequestBody,
@Path(FILE_ID) fileId: String
): Call<FileRemoteResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ internal class NonGmsFileRepositoryImpl(

override fun open() = Unit

override fun update() = Unit
override fun updateFile(localFileToUpload: File, fileId: String, parentId: String?) =
dataSource.updateFile(localFileToUpload, fileId, parentId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,51 @@ internal class NonGmsFileRemoteDataSourceImpl(private val retrofitImpl: GoogleRe
throw (OmhStorageException.DownloadException(DOWNLOAD_GOOGLE_WORKSPACE_ERROR, errorBody))
}
}
override fun updateFile(
localFileToUpload: File,
fileId: String,
parentId: String?
): OmhFile? {
val jsonMetaData = JSONObject().apply {
put(FILE_NAME_KEY, localFileToUpload.name)
put(FILE_PARENTS_KEY, parentId.isNullOrBlank())
put("description", "TEST HANS VIII")
}

val jsonRequestBody = jsonMetaData.toString().toRequestBody(JSON_MIME_TYPE)
val response = retrofitImpl
.getGoogleStorageApiService()
.updateMetaData(jsonRequestBody, fileId)
.execute()

return if (response.isSuccessful) {
val omhFile = response.body()?.toFile()
updateMediaFile(localFileToUpload, omhFile)
} else {
null
}
}

private fun updateMediaFile(
localFileToUpload: File,
omhFile: OmhFile?
): OmhFile? {
if (omhFile == null) {
return null
}

val mimeType = omhFile.mimeType.toMediaTypeOrNull()
val requestFile = localFileToUpload.asRequestBody(mimeType)

val response = retrofitImpl
.getGoogleStorageApiService()
.updateFile(requestFile, omhFile.id)
.execute()

return if (response.isSuccessful) {
response.body()?.toFile()
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import com.omh.android.storage.api.domain.usecase.GetFilesListUseCase
import com.omh.android.storage.api.domain.usecase.GetFilesListUseCaseParams
import com.omh.android.storage.api.domain.usecase.GetFilesListUseCaseResult
import com.omh.android.storage.api.domain.usecase.OmhResult
import com.omh.android.storage.api.domain.usecase.UpdateFileUseCase
import com.omh.android.storage.api.domain.usecase.UpdateFileUseCaseParams
import com.omh.android.storage.api.domain.usecase.UpdateFileUseCaseResult
import com.omh.android.storage.api.domain.usecase.UploadFileUseCase
import com.omh.android.storage.api.domain.usecase.UploadFileUseCaseParams
import com.omh.android.storage.api.domain.usecase.UploadFileUseCaseResult
Expand Down Expand Up @@ -84,4 +87,17 @@ abstract class OmhStorageClient protected constructor(
result
}
}

fun updateFile(
localFileToUpload: File,
fileId: String,
parentId: String?
): OmhTask<UpdateFileUseCaseResult> {
val updateFileUseCase = UpdateFileUseCase(getRepository())
return OmhStorageTaskImpl {
val parameters = UpdateFileUseCaseParams(localFileToUpload, fileId, parentId)
val result = updateFileUseCase(parameters)
result
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ interface OmhFileRemoteDataSource {
fun uploadFile(localFileToUpload: File, parentId: String?): OmhFile?

fun downloadFile(fileId: String, mimeType: String?): ByteArrayOutputStream

fun updateFile(localFileToUpload: File, fileId: String, parentId: String?): OmhFile?
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ interface OmhFileRepository {

fun open()

fun update()
fun updateFile(localFileToUpload: File, fileId: String, parentId: String?): OmhFile?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.omh.android.storage.api.domain.usecase

import com.omh.android.storage.api.domain.model.OmhFile
import com.omh.android.storage.api.domain.repository.OmhFileRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import java.io.File

class UpdateFileUseCase(
private val repository: OmhFileRepository,
dispatcher: CoroutineDispatcher = Dispatchers.Default
) : OmhSuspendUseCase<UpdateFileUseCaseParams, UpdateFileUseCaseResult>(dispatcher) {

override suspend fun execute(parameters: UpdateFileUseCaseParams): UpdateFileUseCaseResult {
return UpdateFileUseCaseResult(
repository.updateFile(parameters.localFileToUpdate, parameters.fileId, parameters.parentId)
)
}
}

data class UpdateFileUseCaseParams(val localFileToUpdate: File, val fileId: String, val parentId: String?)

data class UpdateFileUseCaseResult(val file: OmhFile?)

0 comments on commit a20c178

Please sign in to comment.