1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.storage
21 
22 import cats.implicits._
23 import kamon.annotation.api.Trace
24 import org.make.api.technical.IdGeneratorComponent
25 import org.make.core.DateHelper
26 import org.make.core.user.UserType
27 import org.make.swift.model.Bucket
28 
29 import scala.concurrent.ExecutionContext.Implicits.global
30 import scala.concurrent.Future
31 
32 trait DefaultStorageServiceComponent extends StorageServiceComponent {
33   self: SwiftClientComponent with StorageConfigurationComponent with IdGeneratorComponent =>
34 
35   override lazy val storageService: StorageService = new DefaultStorageService
36 
37   class DefaultStorageService extends StorageService {
38 
39     @Trace(operationName = "client-uploadFile")
40     override def uploadImage(
41       fileType: FileType,
42       name: String,
43       contentType: String,
44       content: Content
45     ): Future[String] = {
46       val path = s"${fileType.path}/$name"
47       swiftClient
48         .sendFile(Bucket(0, 0, storageConfiguration.bucketName), path, contentType, content.toByteArray())
49         .as(s"${storageConfiguration.baseUrl}/assets/$path")
50     }
51 
52     @Trace(operationName = "client-uploadUserAvatar")
53     override def uploadUserAvatar(extension: String, contentType: String, content: Content): Future[String] = {
54       val date = DateHelper.now()
55       val name = s"${date.getYear}/${date.getMonthValue}/${idGenerator.nextId()}.$extension"
56       storageService.uploadImage(FileType.Avatar, name, contentType, content)
57 
58     }
59 
60     @Trace(operationName = "client-uploadAdminUserAvatar")
61     override def uploadAdminUserAvatar(
62       extension: String,
63       contentType: String,
64       content: Content,
65       userType: UserType
66     ): Future[String] = {
67       val date = DateHelper.now()
68       val name =
69         s"${date.getYear}/${date.getMonthValue}/${userType.value.toLowerCase}/${idGenerator.nextId()}.$extension"
70       storageService.uploadImage(FileType.Avatar, name, contentType, content)
71 
72     }
73 
74     @Trace(operationName = "client-uploadReport")
75     override def uploadReport(
76       fileType: FileType,
77       name: String,
78       contentType: String,
79       content: Content
80     ): Future[String] = {
81       val path = s"$name"
82       swiftClient
83         .sendFile(Bucket(0, 0, storageConfiguration.reportBucketName), path, contentType, content.toByteArray())
84         .as(s"${storageConfiguration.baseUrl}/consultations/$path")
85     }
86   }
87 }
Line Stmt Id Pos Tree Symbol Tests Code
48 23386 1667 - 1712 Apply org.make.swift.model.Bucket.apply org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.bucketName)
48 27148 1666 - 1666 ApplyToImplicitArgs cats.instances.FutureInstances.catsStdInstancesForFuture cats.implicits.catsStdInstancesForFuture(scala.concurrent.ExecutionContext.Implicits.global)
48 25278 1680 - 1711 Select org.make.api.technical.storage.StorageConfiguration.bucketName DefaultStorageServiceComponent.this.storageConfiguration.bucketName
48 27610 1677 - 1678 Literal <nosymbol> 0L
48 25672 1637 - 1755 Apply org.make.swift.SwiftClient.sendFile DefaultStorageServiceComponent.this.swiftClient.sendFile(org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.bucketName), path, contentType, content.toByteArray())
48 23782 1674 - 1675 Literal <nosymbol> 0
48 27023 1733 - 1754 Apply org.make.api.technical.storage.Content.toByteArray content.toByteArray()
48 23479 1666 - 1666 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
49 25270 1637 - 1816 Apply cats.Functor.Ops.as cats.implicits.toFunctorOps[scala.concurrent.Future, Unit](DefaultStorageServiceComponent.this.swiftClient.sendFile(org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.bucketName), path, contentType, content.toByteArray()))(cats.implicits.catsStdInstancesForFuture(scala.concurrent.ExecutionContext.Implicits.global)).as[String](("".+(DefaultStorageServiceComponent.this.storageConfiguration.baseUrl).+("/assets/").+(path): String))
54 23794 2007 - 2023 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
56 27618 2150 - 2165 Select org.make.api.technical.storage.FileType.Avatar FileType.Avatar
56 25580 2123 - 2194 Apply org.make.api.technical.storage.StorageService.uploadImage DefaultStorageServiceComponent.this.storageService.uploadImage(FileType.Avatar, name, contentType, content)
67 23020 2446 - 2462 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
70 25682 2600 - 2671 Apply org.make.api.technical.storage.StorageService.uploadImage DefaultStorageServiceComponent.this.storageService.uploadImage(FileType.Avatar, name, contentType, content)
70 26993 2627 - 2642 Select org.make.api.technical.storage.FileType.Avatar FileType.Avatar
83 27546 3017 - 3038 Apply org.make.api.technical.storage.Content.toByteArray content.toByteArray()
83 23700 2952 - 2953 Literal <nosymbol> 0
83 22940 2945 - 2996 Apply org.make.swift.model.Bucket.apply org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.reportBucketName)
83 27004 2944 - 2944 ApplyToImplicitArgs cats.instances.FutureInstances.catsStdInstancesForFuture cats.implicits.catsStdInstancesForFuture(scala.concurrent.ExecutionContext.Implicits.global)
83 23336 2944 - 2944 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
83 27454 2955 - 2956 Literal <nosymbol> 0L
83 24900 2958 - 2995 Select org.make.api.technical.storage.StorageConfiguration.reportBucketName DefaultStorageServiceComponent.this.storageConfiguration.reportBucketName
83 25591 2915 - 3039 Apply org.make.swift.SwiftClient.sendFile DefaultStorageServiceComponent.this.swiftClient.sendFile(org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.reportBucketName), path, contentType, content.toByteArray())
84 25904 2915 - 3107 Apply cats.Functor.Ops.as cats.implicits.toFunctorOps[scala.concurrent.Future, Unit](DefaultStorageServiceComponent.this.swiftClient.sendFile(org.make.swift.model.Bucket.apply(0, 0L, DefaultStorageServiceComponent.this.storageConfiguration.reportBucketName), path, contentType, content.toByteArray()))(cats.implicits.catsStdInstancesForFuture(scala.concurrent.ExecutionContext.Implicits.global)).as[String](("".+(DefaultStorageServiceComponent.this.storageConfiguration.baseUrl).+("/consultations/").+(path): String))