-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): fill content-type based on file extension (#400)
- Loading branch information
Showing
3 changed files
with
60 additions
and
12 deletions.
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,53 @@ | ||
// | ||
// Helpers.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 22/05/24. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(CoreServices) | ||
import CoreServices | ||
#endif | ||
|
||
#if canImport(UniformTypeIdentifiers) | ||
import UniformTypeIdentifiers | ||
#endif | ||
|
||
#if os(Linux) || os(Windows) | ||
/// On Linux or Windows this method always returns `application/octet-stream`. | ||
func mimeTypeForExtension(_: String) -> String { | ||
"application/octet-stream" | ||
} | ||
#else | ||
func mimeTypeForExtension(_ fileExtension: String) -> String { | ||
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, visionOS 1.0, *) { | ||
return UTType(filenameExtension: fileExtension)?.preferredMIMEType ?? "application/octet-stream" | ||
} else { | ||
guard | ||
let type = UTTypeCreatePreferredIdentifierForTag( | ||
kUTTagClassFilenameExtension, | ||
fileExtension as NSString, | ||
nil | ||
)?.takeUnretainedValue(), | ||
let mimeType = UTTypeCopyPreferredTagWithClass( | ||
type, | ||
kUTTagClassMIMEType | ||
)?.takeUnretainedValue() | ||
else { return "application/octet-stream" } | ||
|
||
return mimeType as String | ||
} | ||
} | ||
#endif | ||
|
||
extension String { | ||
var pathExtension: String { | ||
(self as NSString).pathExtension | ||
} | ||
|
||
var fileName: String { | ||
(self as NSString).lastPathComponent | ||
} | ||
} |
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