-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat] TT-39 - Connect with Google Cloud storage #1
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5af8993
Add upload and download locally
Appli-chic 0baf8dd
Create Route.mediaRoutes()
cyrillrx 460d046
Change bucket to 'cheerz_medias'
cyrillrx 309dd30
Add env variable
Appli-chic 69f8d78
Merge branch 'TT-39-config-gcloud' of github.com:cheerz/kotlin-media-…
Appli-chic 9fdcac3
wip upload gcloud
Appli-chic 682ed4c
Add content type when uploading
Appli-chic 31bf264
Fix get all medias
Appli-chic d10e987
Download from google cloud
Appli-chic e5d3b8a
Fix bug downloading wrong image
Appli-chic 481588d
Add .github config
cyrillrx 44be64d
Merge branch 'TT-39-config-gcloud' of github.com:cheerz/kotlin-media-…
cyrillrx 808c59a
Create PipelineContext<*, ApplicationCall>.getBucket()
cyrillrx 7486406
Code cleanup
cyrillrx 4c3a53d
IDE formatting
cyrillrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## 📝 Description | ||
|
||
## 🖼️ Media | ||
|
||
## ✅ Checklist | ||
* [ ] PR has been proofread by the author | ||
|
||
https://cheerz0.atlassian.net/browse/ |
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,20 @@ | ||
name: "Lint PR Title" | ||
|
||
on: | ||
pull_request: | ||
types: | ||
- opened | ||
- edited | ||
- reopened | ||
- synchronize | ||
|
||
jobs: | ||
main: | ||
name: Validate PR title | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: pragmatic-tools/pr-title-validator@1.0.0 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
pattern: ^(\[(Feat|Fix|Tech|UI)\] ([A-Z\d]+)-\d+ - .+)|(Bump version \d{4}\.\d+\.\d+)|(Release \d{4}\.\d+\.\d+)$ |
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
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/cheerz/mediamanager/models/CheerzResponse.kt
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,10 @@ | ||
package com.cheerz.mediamanager.models | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CheerzResponse<Payload>( | ||
@SerialName("response") val response: Payload, | ||
@SerialName("error_message") val errorMessage: String?, | ||
) |
13 changes: 10 additions & 3 deletions
13
src/main/kotlin/com/cheerz/mediamanager/models/MediaItem.kt
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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
package com.cheerz.mediamanager.models | ||
|
||
import com.google.cloud.storage.Blob | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class MediaItem( | ||
val id: String, | ||
val type: MediaType | ||
@SerialName("id") val id: String, | ||
@SerialName("type") val type: MediaType, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🕐 Il faudrait ajouter l'uri pour la renvoyer dans la réponse API |
||
) | ||
|
||
enum class MediaType { IMAGE, VIDEO } | ||
fun Blob.toMediaItem() = MediaItem(name, MediaType.IMAGE) | ||
|
||
enum class MediaType { IMAGE, VIDEO } |
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
package com.cheerz.mediamanager.plugins | ||
|
||
import com.cheerz.mediamanager.routes.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.routing.routing | ||
|
||
fun Application.configureRouting() { | ||
routing { | ||
homeRoute() | ||
|
||
listMediaRoute() | ||
getMediaRoute() | ||
|
||
testSerializationRoute() | ||
mediaRoutes() | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
src/main/kotlin/com/cheerz/mediamanager/plugins/Serialization.kt
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
12 changes: 0 additions & 12 deletions
12
src/main/kotlin/com/cheerz/mediamanager/routes/Experimentations.kt
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.cheerz.mediamanager.routes | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.application.call | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.get | ||
|
||
fun Route.homeRoute() { | ||
get("/") { | ||
call.respondText("Hello World!") | ||
} | ||
} | ||
} |
116 changes: 97 additions & 19 deletions
116
src/main/kotlin/com/cheerz/mediamanager/routes/MediaRoutes.kt
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 |
---|---|---|
@@ -1,26 +1,104 @@ | ||
package com.cheerz.mediamanager.routes | ||
|
||
import com.cheerz.mediamanager.storage.mediaStorage | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
fun Route.listMediaRoute() { | ||
get("/media") { | ||
if (mediaStorage.isNotEmpty()) { | ||
call.respond(mediaStorage) | ||
} | ||
import com.cheerz.mediamanager.models.CheerzResponse | ||
import com.cheerz.mediamanager.models.MediaItem | ||
import com.cheerz.mediamanager.models.MediaType | ||
import com.cheerz.mediamanager.models.toMediaItem | ||
import com.cheerz.mediamanager.storage | ||
import com.google.cloud.storage.Bucket | ||
import com.google.cloud.storage.Storage | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.content.PartData | ||
import io.ktor.http.content.forEachPart | ||
import io.ktor.http.content.streamProvider | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.application.call | ||
import io.ktor.server.request.receiveMultipart | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.response.respondBytes | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.post | ||
import io.ktor.server.routing.route | ||
import io.ktor.util.pipeline.PipelineContext | ||
|
||
|
||
fun Route.mediaRoutes() = route("/media") { | ||
listMedias(contentType = "image") | ||
listMedias(contentType = "video") | ||
upload() | ||
download() | ||
} | ||
|
||
private fun Route.listMedias(contentType: String) { | ||
get("/${contentType}s") { | ||
val buckets = getBucket().list(Storage.BlobListOption.currentDirectory()).values | ||
val medias = buckets | ||
.filter { it.contentType?.startsWith(contentType) == true } | ||
.map { it.toMediaItem() } | ||
|
||
call.response.status(HttpStatusCode.OK) | ||
call.respond( | ||
CheerzResponse(medias, null) | ||
) | ||
} | ||
} | ||
|
||
fun Route.getMediaRoute() { | ||
get("/media/{id?}") { | ||
val id = call.parameters["id"] ?: return@get call.respondText("Bad Request", status = HttpStatusCode.BadRequest) | ||
val media = mediaStorage.find { it.id == id } ?: return@get call.respondText( | ||
"Not Found", | ||
status = HttpStatusCode.NotFound | ||
private fun Route.upload() { | ||
post("/upload") { | ||
val multipart = call.receiveMultipart() | ||
multipart.forEachPart { part -> | ||
when (part) { | ||
is PartData.FileItem -> { | ||
val uuid = java.util.UUID.randomUUID().toString() | ||
val fileBytes = part.streamProvider().readBytes() | ||
|
||
getBucket().create(uuid, fileBytes, part.contentType.toString()) | ||
|
||
val media = MediaItem(uuid, MediaType.IMAGE) | ||
call.response.status(HttpStatusCode.Created) | ||
call.respond( | ||
CheerzResponse(media, null) | ||
) | ||
} | ||
|
||
else -> { | ||
call.response.status(HttpStatusCode.BadRequest) | ||
call.respond( | ||
CheerzResponse(null, "Bad request") | ||
) | ||
} | ||
} | ||
part.dispose() | ||
} | ||
|
||
call.response.status(HttpStatusCode.BadRequest) | ||
call.respond( | ||
CheerzResponse(null, "Bad request") | ||
) | ||
call.respond(media) | ||
} | ||
} | ||
} | ||
|
||
private fun Route.download() { | ||
get("/download/{id}") { | ||
val id = call.parameters["id"] | ||
|
||
val blob = getBucket().get(id) | ||
|
||
if (blob == null) { | ||
call.response.status(HttpStatusCode.NotFound) | ||
call.respond( | ||
CheerzResponse(null, "Not found") | ||
) | ||
return@get | ||
} | ||
|
||
call.respondBytes(blob.getContent(), ContentType.Image.JPEG, HttpStatusCode.OK) | ||
} | ||
} | ||
|
||
private fun PipelineContext<*, ApplicationCall>.getBucket(): Bucket { | ||
val bucketName = call.application.environment.config.property("ktor.storage.bucket_name").getString() | ||
return storage.get(bucketName) | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/kotlin/com/cheerz/mediamanager/storage/RamMediaStorage.kt
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
ktor { | ||
deployment { | ||
port = 8080 | ||
port = ${?PORT} | ||
} | ||
application { | ||
modules = [ com.cheerz.mediamanager.ApplicationKt.module ] | ||
} | ||
storage { | ||
bucket_name = "cheerz_medias" | ||
bucket_name = ${?BUCKET_NAME} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Il faudra qu'on regarde pour bien gérer le storage.