Refactor Uploader.upload(LocalFile,Bucket,Boolean,UploadEventListener,Int) (#129)
* [storate-api] Remove unused tryCount * [storage-aws] Uploader Reduce the number of parameters
This commit is contained in:
parent
899e0724c9
commit
4a54038933
7 changed files with 54 additions and 63 deletions
|
@ -36,8 +36,7 @@ case class UnversionedMirrorArchive(syncTotals: SyncTotals)
|
|||
Storage.upload(
|
||||
localFile,
|
||||
bucket,
|
||||
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar),
|
||||
1)
|
||||
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar))
|
||||
}
|
||||
|
||||
object UnversionedMirrorArchive {
|
||||
|
|
|
@ -20,10 +20,11 @@ case class DummyStorageService(s3ObjectData: S3ObjectsData,
|
|||
): TaskR[Console, S3ObjectsData] =
|
||||
TaskR(s3ObjectData)
|
||||
|
||||
override def upload(localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int): UIO[StorageQueueEvent] = {
|
||||
override def upload(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
): UIO[StorageQueueEvent] = {
|
||||
val (remoteKey, md5Hash) = uploadFiles(localFile.file)
|
||||
UIO(StorageQueueEvent.UploadQueueEvent(remoteKey, md5Hash))
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ object Storage {
|
|||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int
|
||||
): ZIO[Storage with Config, Nothing, StorageQueueEvent]
|
||||
|
||||
def copy(
|
||||
|
@ -57,8 +56,8 @@ object Storage {
|
|||
override def upload(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int): ZIO[Storage, Nothing, StorageQueueEvent] =
|
||||
uploadEventListener: UploadEventListener
|
||||
): ZIO[Storage, Nothing, StorageQueueEvent] =
|
||||
uploadResult
|
||||
|
||||
override def copy(
|
||||
|
@ -99,11 +98,9 @@ object Storage {
|
|||
final def upload(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int
|
||||
uploadEventListener: UploadEventListener
|
||||
): ZIO[Storage with Config, Nothing, StorageQueueEvent] =
|
||||
ZIO.accessM(
|
||||
_.storage upload (localFile, bucket, uploadEventListener, tryCount))
|
||||
ZIO.accessM(_.storage upload (localFile, bucket, uploadEventListener))
|
||||
|
||||
final def copy(
|
||||
bucket: Bucket,
|
||||
|
|
|
@ -28,11 +28,9 @@ object S3Storage {
|
|||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
Uploader.upload(transferManager)(localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
1)
|
||||
): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
Uploader.upload(transferManager)(
|
||||
Uploader.Request(localFile, bucket, uploadEventListener))
|
||||
|
||||
override def copy(bucket: Bucket,
|
||||
sourceKey: RemoteKey,
|
||||
|
|
|
@ -18,24 +18,24 @@ import zio.{UIO, ZIO}
|
|||
|
||||
trait Uploader {
|
||||
|
||||
def upload(transferManager: => AmazonTransferManager)(
|
||||
case class Request(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int
|
||||
): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
transfer(transferManager)(localFile, bucket, uploadEventListener)
|
||||
.catchAll(handleError(localFile.remoteKey))
|
||||
uploadEventListener: UploadEventListener
|
||||
)
|
||||
|
||||
def upload(transferManager: => AmazonTransferManager)(
|
||||
request: Request): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
transfer(transferManager)(request)
|
||||
.catchAll(handleError(request.localFile.remoteKey))
|
||||
|
||||
private def handleError(remoteKey: RemoteKey)(e: Throwable) =
|
||||
UIO(ErrorQueueEvent(Action.Upload(remoteKey.key), remoteKey, e))
|
||||
|
||||
private def transfer(transferManager: => AmazonTransferManager)(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener
|
||||
request: Request
|
||||
) =
|
||||
request(localFile, bucket, progressListener(uploadEventListener)) >>=
|
||||
putObjectRequest(request) >>=
|
||||
dispatch(transferManager)
|
||||
|
||||
private def dispatch(transferManager: AmazonTransferManager)(
|
||||
|
@ -49,18 +49,20 @@ trait Uploader {
|
|||
MD5Hash(uploadResult.getETag)))
|
||||
}
|
||||
|
||||
private def request(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
listener: ProgressListener
|
||||
private def putObjectRequest(
|
||||
request: Request
|
||||
) = {
|
||||
val request =
|
||||
new PutObjectRequest(bucket.name, localFile.remoteKey.key, localFile.file)
|
||||
.withMetadata(metadata(localFile))
|
||||
val putRequest =
|
||||
new PutObjectRequest(request.bucket.name,
|
||||
request.localFile.remoteKey.key,
|
||||
request.localFile.file)
|
||||
.withMetadata(metadata(request.localFile))
|
||||
for {
|
||||
batchMode <- Config.batchMode
|
||||
r = if (batchMode) request
|
||||
else request.withGeneralProgressListener(listener)
|
||||
r = if (batchMode) putRequest
|
||||
else
|
||||
putRequest.withGeneralProgressListener(
|
||||
progressListener(request.uploadEventListener))
|
||||
} yield r
|
||||
}
|
||||
|
||||
|
|
|
@ -25,28 +25,31 @@ trait AmazonS3ClientTestFixture extends MockFactory {
|
|||
|
||||
override def listObjects(
|
||||
bucket: Bucket,
|
||||
prefix: RemoteKey): TaskR[Console, S3ObjectsData] =
|
||||
prefix: RemoteKey
|
||||
): TaskR[Console, S3ObjectsData] =
|
||||
Lister.listObjects(client)(bucket, prefix)
|
||||
|
||||
override def upload(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
Uploader.upload(transferManager)(localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
1)
|
||||
): ZIO[Config, Nothing, StorageQueueEvent] =
|
||||
Uploader.upload(transferManager)(
|
||||
Uploader.Request(localFile, bucket, uploadEventListener))
|
||||
|
||||
override def copy(bucket: Bucket,
|
||||
sourceKey: RemoteKey,
|
||||
hash: MD5Hash,
|
||||
targetKey: RemoteKey): UIO[StorageQueueEvent] =
|
||||
override def copy(
|
||||
bucket: Bucket,
|
||||
sourceKey: RemoteKey,
|
||||
hash: MD5Hash,
|
||||
targetKey: RemoteKey
|
||||
): UIO[StorageQueueEvent] =
|
||||
Copier.copy(client)(
|
||||
Copier.Request(bucket, sourceKey, hash, targetKey))
|
||||
|
||||
override def delete(bucket: Bucket,
|
||||
remoteKey: RemoteKey): UIO[StorageQueueEvent] =
|
||||
override def delete(
|
||||
bucket: Bucket,
|
||||
remoteKey: RemoteKey
|
||||
): UIO[StorageQueueEvent] =
|
||||
Deleter.delete(client)(bucket, remoteKey)
|
||||
|
||||
override def shutdown: UIO[StorageQueueEvent] = {
|
||||
|
|
|
@ -27,7 +27,6 @@ class UploaderTest extends FreeSpec with MockFactory {
|
|||
val remoteKey = RemoteKey("aRemoteKey")
|
||||
val localFile = LocalFile(aFile, aSource, hashes, remoteKey)
|
||||
val bucket = Bucket("aBucket")
|
||||
val tryCount = 1
|
||||
val uploadResult = new UploadResult
|
||||
uploadResult.setKey(remoteKey.key)
|
||||
uploadResult.setETag(aHash.hash)
|
||||
|
@ -47,8 +46,7 @@ class UploaderTest extends FreeSpec with MockFactory {
|
|||
invoke(fixture.amazonS3TransferManager)(
|
||||
localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
tryCount
|
||||
uploadEventListener
|
||||
)
|
||||
assertResult(expected)(result)
|
||||
}
|
||||
|
@ -66,8 +64,7 @@ class UploaderTest extends FreeSpec with MockFactory {
|
|||
invoke(fixture.amazonS3TransferManager)(
|
||||
localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
tryCount
|
||||
uploadEventListener
|
||||
)
|
||||
assertResult(expected)(result)
|
||||
}
|
||||
|
@ -85,8 +82,7 @@ class UploaderTest extends FreeSpec with MockFactory {
|
|||
invoke(fixture.amazonS3TransferManager)(
|
||||
localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
tryCount
|
||||
uploadEventListener
|
||||
)
|
||||
assertResult(expected)(result)
|
||||
}
|
||||
|
@ -94,19 +90,14 @@ class UploaderTest extends FreeSpec with MockFactory {
|
|||
def invoke(transferManager: AmazonTransferManager)(
|
||||
localFile: LocalFile,
|
||||
bucket: Bucket,
|
||||
uploadEventListener: UploadEventListener,
|
||||
tryCount: Int
|
||||
uploadEventListener: UploadEventListener
|
||||
) = {
|
||||
type TestEnv = Config
|
||||
val testEnv: TestEnv = Config.Live
|
||||
new DefaultRuntime {}.unsafeRunSync {
|
||||
Uploader
|
||||
.upload(transferManager)(
|
||||
localFile,
|
||||
bucket,
|
||||
uploadEventListener,
|
||||
tryCount
|
||||
)
|
||||
Uploader.Request(localFile, bucket, uploadEventListener))
|
||||
.provide(testEnv)
|
||||
}.toEither
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue