Skip to content
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

Files in public directory being opened as UTF8 Strings #25

Closed
califrench opened this issue Mar 8, 2016 · 5 comments
Closed

Files in public directory being opened as UTF8 Strings #25

califrench opened this issue Mar 8, 2016 · 5 comments

Comments

@califrench
Copy link
Contributor

I'm trying to load images from the public directory but it seems from looking at the router code that anything loaded from the public directory needs to be source code—like css or js files.

// In Router.swift
if let body = String(data:contents!, encoding: NSUTF8StringEncoding) {
    return Response(.Ok, contentType: "text/plain; charset=utf8", body: body)
}

We should be able to also load images and other assets from that directory. Also CSS files and JS files should return a different mimetype than text/plain.

@califrench
Copy link
Contributor Author

This issue isn't as simple as writing an else statement and returning a different response since responses must have a body of the following type: String?
I'm trying to figure out a solution for this but most likely won't come up with the most graceful one. I'll submit a PR if I think my solution is viable.

@sauliusgrigaitis
Copy link
Owner

@califrench thanks. I believe there is solution for mime types management in Swift, we should not be very first facing this issue :) Please let me know if you will find something.

@califrench
Copy link
Contributor Author

@sauliusgrigaitis I realized that this was highlighting two different issues as I typed my explanation up.
One issue is the fact we are sending all assets with text/plain but the bigger issue is we can only return text resources and not binary files with a Response

califrench added a commit to califrench/Swifton that referenced this issue Mar 10, 2016
This adds support for the `mimeType()` function on `String?` types. The
previous implementation allowed to pass nil values to the internal
`MimeType(ext: String?) -> String` function but `.mimeType()` could
never be called on a nil String because that was an extension on the
`String` type.
This allows us to do this
```swift
let testString :String? = nil
testString.mimeType() //  -> "application/octet-stream"
```
Instead of this
```swift
let testString : String? = nil
(testString ?? "").mimeType() // -> "application/octet-stream"
```
Or worse
```swift
let testString : String? = nil
testString.extension?.mimeType ?? "application/octet-stream" //
->"application/octet-stream"
```
This new implementation is used in the router and fixes sauliusgrigaitis#25
califrench added a commit to califrench/Swifton that referenced this issue Mar 26, 2016
This adds support for the `mimeType()` function on `String?` types. The
previous implementation allowed to pass nil values to the internal
`MimeType(ext: String?) -> String` function but `.mimeType()` could
never be called on a nil String because that was an extension on the
`String` type.
This allows us to do this
```swift
let testString :String? = nil
testString.mimeType() //  -> "application/octet-stream"
```
Instead of this
```swift
let testString : String? = nil
(testString ?? "").mimeType() // -> "application/octet-stream"
```
Or worse
```swift
let testString : String? = nil
testString.extension?.mimeType ?? "application/octet-stream" //
->"application/octet-stream"
```
This new implementation is used in the router and fixes sauliusgrigaitis#25

Forgot semicolon in Router

Changed extension to ``extension``

Updated the Optional extension to allow any kind of optional. Thought it would be unsafe to force cast an Optional of a different type to a String? but turns out to work just fine

Added safety to the optional extension. Casting a non nil Optional type of any other type than String would previously cause a crash.

Oops, forgot parentheses

Updated the Router to use optional chaining and removed the optional extension on MimeType

Added a static test javascript file and updated the tests to check for the mime type served by the router for static files.
califrench added a commit to califrench/Swifton that referenced this issue Mar 27, 2016
@califrench
Copy link
Contributor Author

califrench commented May 30, 2016

This is no longer an issue.
EDIT: I forgot this was referring to files such as images. It is still an issue.

@califrench
Copy link
Contributor Author

This is a duplicate of #28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants