Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #116 from rb-de0/feature/randomize-image-name
Browse files Browse the repository at this point in the history
Randomize image name
  • Loading branch information
rb-de0 committed Oct 27, 2018
2 parents 3ea958d + 2c6a3ec commit 08496f0
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 31 deletions.
37 changes: 14 additions & 23 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/IBM-Swift/BlueCryptor.git",
"state": {
"branch": null,
"revision": "efdb770164a7d3b222d6dcb78f253633c38a2ec2",
"version": "1.0.10"
"revision": "a960efc2759c8816b00baed5db7b3d903a402e78",
"version": "1.0.14"
}
},
{
Expand All @@ -28,15 +28,6 @@
"version": "1.0.1"
}
},
{
"package": "CommonCrypto",
"repositoryURL": "https://github.com/IBM-Swift/CommonCrypto.git",
"state": {
"branch": null,
"revision": "9156d238dbc4c15455b77b45e721b2bb0b995e31",
"version": "1.0.0"
}
},
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
Expand All @@ -51,8 +42,8 @@
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "eb876a758733166a4fb20f3f0a17b480c5ea563e",
"version": "3.4.3"
"revision": "96ce86ebf9198328795c4b9cb711489460be083c",
"version": "3.4.4"
}
},
{
Expand Down Expand Up @@ -87,8 +78,8 @@
"repositoryURL": "https://github.com/vapor/fluent.git",
"state": {
"branch": null,
"revision": "270b6fa372f03809b9795e8f8b9d1c31267a0ff3",
"version": "3.0.0"
"revision": "dc258fe53880f80508df317df3c903ee2c2b9317",
"version": "3.1.0"
}
},
{
Expand All @@ -114,8 +105,8 @@
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "9e3eff9dfa4df7fc282bf27f801c72b3ffbfd984",
"version": "3.1.4"
"revision": "272b22be8cb3364e42a4701c2e0676e37480ec5a",
"version": "3.1.5"
}
},
{
Expand Down Expand Up @@ -159,8 +150,8 @@
"repositoryURL": "https://github.com/vapor-community/pagination.git",
"state": {
"branch": null,
"revision": "d018e2a02ed94536ad494ff750057990e8ae1acf",
"version": "1.0.7"
"revision": "538dcab96e5aa93a22509ad11b1592891fc2ba80",
"version": "1.0.8"
}
},
{
Expand Down Expand Up @@ -258,8 +249,8 @@
"repositoryURL": "https://github.com/scinfu/SwiftSoup.git",
"state": {
"branch": null,
"revision": "489072c6b1f1ebcc650538f55c0faaa627eeb88f",
"version": "1.7.4"
"revision": "11da8c685ddde2d519fad04a7daf8485bbbc773e",
"version": "1.7.5"
}
},
{
Expand Down Expand Up @@ -312,8 +303,8 @@
"repositoryURL": "https://github.com/vapor/websocket.git",
"state": {
"branch": null,
"revision": "149af03348f60ac8b84defdf277112d62fd8c704",
"version": "1.0.2"
"revision": "eb4277f75f1d96a3d15c852cdd89af1799093dcd",
"version": "1.1.0"
}
}
]
Expand Down
20 changes: 20 additions & 0 deletions Sources/App/Application/Services/Helper/ImageNameGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation
import Random
import Vapor

protocol ImageNameGenerator {
func generateImageName(from string: String) throws -> String
}

final class ImageNameGeneratorDefault: ImageNameGenerator, Service {

private let generator: DataGenerator

init(generator: DataGenerator) {
self.generator = generator
}

func generateImageName(from string: String) throws -> String {
return try generator.generateData(count: 16).base64URLEncodedString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Authentication
import Vapor

final class StupidPasswordVeryfier: PasswordVerifier, Service {

func verify(_ password: LosslessDataConvertible, created hash: LosslessDataConvertible) throws -> Bool {
return true
}
}
10 changes: 8 additions & 2 deletions Sources/App/Controllers/API/API+ImageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ extension API {

func store(request: Request, form: ImageUploadForm) throws -> Future<HTTPStatus> {

guard let imageExtension = form.name.split(separator: ".").last else {
return request.future(HTTPStatus.badRequest)
}

let repository = try request.make(ImageRepository.self)
let newImage = try Image(from: form, on: request)
let imageName = try request.make(ImageNameGenerator.self).generateImageName(from: form.name)
let imageFileName = imageName.appending(".").appending(imageExtension)
let newImage = try Image(from: imageFileName, on: request)

let deleteTransaction = request.withPooledConnection(to: .mysql) { conn in

MySQLDatabase.transactionExecute({ transaction in
newImage.save(on: transaction).map { _ in
try repository.save(image: form.data, for: form.name)
try repository.save(image: form.data, for: imageFileName)
}
}, on: conn)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/App/Extension/Environment+Util.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Vapor

extension Environment {

var isDevelopment: Bool {
return name == "development"
}
}
4 changes: 4 additions & 0 deletions Sources/App/Extension/URandom+Service.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Random
import Vapor

extension URandom: Service {}
6 changes: 3 additions & 3 deletions Sources/App/Models/Scheme/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ final class Image: DatabaseModel {
var createdAt: Date?
var updatedAt: Date?

init(from form: ImageUploadForm, on container: Container) throws {
init(from name: String, on container: Container) throws {
let relativePath = try container.make(FileConfig.self).imageRoot
self.path = relativePath.started(with: "/").finished(with: "/").appending(form.name)
self.altDescription = form.name
self.path = relativePath.started(with: "/").finished(with: "/").appending(name)
self.altDescription = name
}

func formPublic(on container: Container) throws -> Public {
Expand Down
17 changes: 16 additions & 1 deletion Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Crypto
import FluentMySQL
import Leaf
import Redis
import Random
import Vapor
import VaporSecurityHeaders

Expand Down Expand Up @@ -32,9 +33,23 @@ public func configure(
return try container.make(ConfigProvider.self).make(NIOServerConfig.self)
}

// data
let random = try URandom()
services.register(random, as: DataGenerator.self)

// generator
services.register(ImageNameGenerator.self) { container in
ImageNameGeneratorDefault(generator: try container.make())
}

// bcrypt
try services.register(AuthenticationProvider())
config.prefer(BCryptDigest.self, for: PasswordVerifier.self)
if environment.isDevelopment {
services.register(StupidPasswordVeryfier(), as: PasswordVerifier.self)
config.prefer(StupidPasswordVeryfier.self, for: PasswordVerifier.self)
} else {
config.prefer(BCryptDigest.self, for: PasswordVerifier.self)
}

// router
let router = EngineRouter.default()
Expand Down
2 changes: 1 addition & 1 deletion Tests/YubatakeTests/TestTools/ApplicationBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ApplicationBuilder {
class func build(forAdminTests: Bool, envArgs: [String]? = nil, customize: ((Config, Services) -> (Config, Services))? = nil) throws -> Application {

var config = Config.default()
var env = try Environment.detect()
var env = Environment(name: "test", isRelease: false)
var services = Services.default()

if let environmentArgs = envArgs {
Expand Down
2 changes: 1 addition & 1 deletion Tests/YubatakeTests/TestTools/DataMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class DataMaker {

class func makeImage(_ name: String, on container: Container) throws -> (ImageUploadForm, Image){
let form = try decode(ImageUploadForm.self, from: ["image_file_data": "data", "image_file_name": name])
let image = try Image(from: form, on: container)
let image = try Image(from: name, on: container)
return (form, image)
}

Expand Down

0 comments on commit 08496f0

Please sign in to comment.