-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #800 from eikek/file-backends
File backends
- Loading branch information
Showing
42 changed files
with
3,160 additions
and
204 deletions.
There are no files selected for viewing
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
19 changes: 0 additions & 19 deletions
19
modules/backend/src/main/scala/sharry/backend/Config.scala
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
modules/backend/src/main/scala/sharry/backend/config/Config.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sharry.backend.config | ||
|
||
import cats.data.ValidatedNec | ||
import cats.syntax.all._ | ||
|
||
import sharry.backend.auth.AuthConfig | ||
import sharry.backend.job.CleanupConfig | ||
import sharry.backend.mail.MailConfig | ||
import sharry.backend.share.ShareConfig | ||
import sharry.backend.signup.SignupConfig | ||
import sharry.store.{ComputeChecksumConfig, JdbcConfig} | ||
|
||
case class Config( | ||
jdbc: JdbcConfig, | ||
signup: SignupConfig, | ||
auth: AuthConfig, | ||
share: ShareConfig, | ||
cleanup: CleanupConfig, | ||
mail: MailConfig, | ||
files: FilesConfig, | ||
computeChecksum: ComputeChecksumConfig | ||
) { | ||
|
||
def validate: ValidatedNec[String, Config] = | ||
(files.validate, computeChecksum.validate) | ||
.mapN((fc, cc) => copy(files = fc, computeChecksum = cc)) | ||
} |
17 changes: 17 additions & 0 deletions
17
modules/backend/src/main/scala/sharry/backend/config/CopyFilesConfig.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sharry.backend.config | ||
|
||
import cats.data.{Validated, ValidatedNec} | ||
|
||
import sharry.common.Ident | ||
|
||
case class CopyFilesConfig( | ||
enable: Boolean, | ||
source: Ident, | ||
target: Ident, | ||
parallel: Int | ||
) { | ||
|
||
def validate: ValidatedNec[String, Unit] = | ||
if (source == target) Validated.invalidNec("Source and target must not be the same") | ||
else Validated.validNec(()) | ||
} |
54 changes: 54 additions & 0 deletions
54
modules/backend/src/main/scala/sharry/backend/config/FilesConfig.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sharry.backend.config | ||
|
||
import cats.data.{Validated, ValidatedNec} | ||
import cats.syntax.all._ | ||
|
||
import sharry.common.Ident | ||
import sharry.store.FileStoreConfig | ||
|
||
case class FilesConfig( | ||
defaultStore: Ident, | ||
stores: Map[Ident, FileStoreConfig], | ||
copyFiles: CopyFilesConfig | ||
) { | ||
|
||
val enabledStores: Map[Ident, FileStoreConfig] = | ||
stores.view.filter(_._2.enabled).toMap | ||
|
||
def defaultStoreConfig: FileStoreConfig = | ||
enabledStores.getOrElse( | ||
defaultStore, | ||
sys.error(s"Store '${defaultStore.id}' not found. Is it enabled?") | ||
) | ||
|
||
def validate: ValidatedNec[String, FilesConfig] = { | ||
val storesEmpty = | ||
if (enabledStores.isEmpty) | ||
Validated.invalidNec( | ||
"No file stores defined! Make sure at least one enabled store is present." | ||
) | ||
else Validated.validNec(()) | ||
|
||
val defaultStorePresent = | ||
enabledStores.get(defaultStore) match { | ||
case Some(_) => Validated.validNec(()) | ||
case None => | ||
Validated.invalidNec(s"Default file store not present: ${defaultStore}") | ||
} | ||
|
||
val validCopyStores = | ||
if (!copyFiles.enable) Validated.validNec(()) | ||
else { | ||
val exist = enabledStores.contains(copyFiles.source) && | ||
enabledStores.contains(copyFiles.target) | ||
if (exist) Validated.validNec(()) | ||
else | ||
Validated.invalidNec( | ||
s"The source or target name for the copy-files section doesn't exist in the list of enabled file stores." | ||
) | ||
} | ||
|
||
(storesEmpty |+| defaultStorePresent |+| validCopyStores |+| copyFiles.validate) | ||
.map(_ => this) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
modules/backend/src/main/scala/sharry/backend/files/OFiles.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package sharry.backend.files | ||
|
||
import cats.data.OptionT | ||
import cats.effect._ | ||
import cats.syntax.all._ | ||
|
||
import sharry.backend.config.FilesConfig | ||
import sharry.common.Ident | ||
import sharry.store.{FileStoreConfig, Store} | ||
|
||
import binny.{AttributeName, CopyTool} | ||
|
||
trait OFiles[F[_]] { | ||
|
||
def computeBackgroundChecksum: Resource[F, F[Outcome[F, Throwable, Unit]]] | ||
|
||
def copyFiles(source: FileStoreConfig, target: FileStoreConfig): F[Int] | ||
|
||
def copyFiles(source: Ident, target: Ident): F[Int] | ||
} | ||
|
||
object OFiles { | ||
|
||
def apply[F[_]: Async]( | ||
store: Store[F], | ||
fileConfig: FilesConfig | ||
): OFiles[F] = | ||
new OFiles[F] { | ||
private[this] val logger = sharry.logging.getLogger[F] | ||
|
||
def computeBackgroundChecksum: Resource[F, F[Outcome[F, Throwable, Unit]]] = | ||
Async[F].background( | ||
store.fileStore.computeAttributes | ||
.consumeAll(AttributeName.all) | ||
.evalMap(store.fileStore.updateChecksum) | ||
.compile | ||
.drain | ||
) | ||
|
||
def copyFiles(source: Ident, target: Ident): F[Int] = | ||
(for { | ||
src <- OptionT.fromOption[F](fileConfig.enabledStores.get(source)) | ||
trg <- OptionT.fromOption[F](fileConfig.enabledStores.get(target)) | ||
r <- OptionT.liftF(copyFiles(src, trg)) | ||
} yield r).getOrElseF( | ||
Sync[F].raiseError( | ||
new IllegalArgumentException( | ||
s"Source or target store not found for keys: ${source.id} and ${target.id}" | ||
) | ||
) | ||
) | ||
|
||
def copyFiles(source: FileStoreConfig, target: FileStoreConfig): F[Int] = { | ||
val src = store.fileStore.createBinaryStore(source) | ||
val trg = store.fileStore.createBinaryStore(target) | ||
val binnyLogger = FileStoreConfig.SharryLogger(logger) | ||
|
||
logger.info(s"Starting to copy $source -> $target") *> | ||
CopyTool | ||
.copyAll( | ||
binnyLogger, | ||
src, | ||
trg, | ||
store.fileStore.chunkSize, | ||
fileConfig.copyFiles.parallel | ||
) | ||
.flatTap { r => | ||
logger.info( | ||
s"Copied ${r.success} files, ${r.exist} existed already and ${r.notFound} were not found." | ||
) *> (if (r.failed.nonEmpty) | ||
logger.warn(s"Failed to copy these files: ${r.failed}") | ||
else ().pure[F]) | ||
} | ||
.map(_.success) | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.