This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
Get the MIME type from File and UrlEncode #442
Open
tobiasschuerg
wants to merge
12
commits into
android:master
Choose a base branch
from
tobiasschuerg:file_mime_type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c059a73
Added File.getMimeType extension.
tobiasschuerg fab7160
Added test for jepd and png.
tobiasschuerg 708e327
Updated api.
tobiasschuerg 6faf0b6
Added two more tests for missing and unknown extension.
tobiasschuerg 4601222
Url encodes the path plus test for a path with spaces.
tobiasschuerg d446f31
Updated api.
tobiasschuerg 3460aac
Renaming and formatting.
tobiasschuerg 3ba0756
Merge branch 'master' into file_mime_type
tobiasschuerg 408b1e6
Renamed method getMimeType() a property and renamed it to mimeTypeFro…
tobiasschuerg 6a1f9b9
Updated api.
tobiasschuerg 24276d9
Merge branch 'file_mime_type' of https://github.com/tobiasschuerg/and…
tobiasschuerg ce47ff5
Removed empty line.
tobiasschuerg 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
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,55 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.io | ||
|
||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertSame | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class FileTest { | ||
|
||
@Test fun jpg() { | ||
val photo = File("sunset.jpg") | ||
val type = photo.getMimeType() | ||
assertSame("image/jpeg", type) | ||
} | ||
|
||
@Test fun png() { | ||
val logo = File("images/logo.png") | ||
val type = logo.getMimeType() | ||
assertSame("image/png", type) | ||
} | ||
|
||
@Test fun pdfWithSpaces() { | ||
val thesis = File("my documents/master thesis-v5.1-final-final2.pdf") | ||
val type = thesis.getMimeType() | ||
assertSame("application/pdf", type) | ||
} | ||
|
||
@Test fun noExtension() { | ||
val path = File("foo/bar") | ||
val type = path.getMimeType() | ||
assertNull(type) | ||
} | ||
|
||
@Test fun unknownExtension() { | ||
val unknownFile = File("foo/bar.baz") | ||
val type = unknownFile.getMimeType() | ||
assertNull(type) | ||
} | ||
} |
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,32 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.io | ||
|
||
import android.webkit.MimeTypeMap | ||
import androidx.core.text.urlEncode | ||
import java.io.File | ||
|
||
/** | ||
* Returns the MIME type (content type) of this [File]. | ||
* | ||
* @see MimeUtils | ||
* @return The MIME type for the files' extension or null iff there is none. | ||
*/ | ||
fun File.getMimeType(): String? { | ||
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. The syntax seems confusing:
I'd suggest to declare it as |
||
return MimeTypeMap.getFileExtensionFromUrl(toString().urlEncode()) | ||
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. IMHO, We can use path instead of toString() as it easy to understand. |
||
?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } | ||
} |
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 |
---|---|---|
|
@@ -14,10 +14,13 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
// Aliases to public API. | ||
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. nit: Remove extra spaces. Keep this consistent as others. |
||
|
||
package androidx.core.text | ||
|
||
import android.net.Uri | ||
import android.text.TextUtils | ||
|
||
/** | ||
|
@@ -26,3 +29,10 @@ import android.text.TextUtils | |
* @see TextUtils.htmlEncode | ||
*/ | ||
inline fun String.htmlEncode(): String = TextUtils.htmlEncode(this) | ||
|
||
/** | ||
* Url-encode the string. | ||
* | ||
* @see Uri.encode | ||
*/ | ||
inline fun String.urlEncode(): String = Uri.encode(this) |
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.
nit: There should be "if" instead of "iff"