Skip to content

Commit e795b62

Browse files
Ondrej RafajOndrej Rafaj
authored andcommitted
new helper methods + examples
1 parent 9a80fee commit e795b62

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

Generator/MimeLibGenerator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
156C17551E020B3200101575 /* Mime+Tools.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156C17521E020B3200101575 /* Mime+Tools.swift */; };
1212
156C17561E020B3200101575 /* Mime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156C17531E020B3200101575 /* Mime.swift */; };
1313
156C17571E020B3200101575 /* MimeType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156C17541E020B3200101575 /* MimeType.swift */; };
14+
156C175E1E0211AB00101575 /* Example.swift in Sources */ = {isa = PBXBuildFile; fileRef = 156C175D1E0211AB00101575 /* Example.swift */; };
1415
15FBD0631E01F3CE00E75B63 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15FBD0621E01F3CE00E75B63 /* main.swift */; };
1516
15FBD06A1E01F41200E75B63 /* String+Tools.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15FBD0691E01F41200E75B63 /* String+Tools.swift */; };
1617
/* End PBXBuildFile section */
@@ -35,6 +36,7 @@
3536
156C17581E020B7F00101575 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
3637
156C17591E020B7F00101575 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
3738
156C175B1E0210CC00101575 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Package.swift; path = ../Package.swift; sourceTree = "<group>"; };
39+
156C175D1E0211AB00101575 /* Example.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Example.swift; sourceTree = "<group>"; };
3840
15FBD05F1E01F3CE00E75B63 /* MimeLibGenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = MimeLibGenerator; sourceTree = BUILT_PRODUCTS_DIR; };
3941
15FBD0621E01F3CE00E75B63 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
4042
15FBD0691E01F41200E75B63 /* String+Tools.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Tools.swift"; sourceTree = "<group>"; };
@@ -88,6 +90,7 @@
8890
15FBD0691E01F41200E75B63 /* String+Tools.swift */,
8991
15FBD0621E01F3CE00E75B63 /* main.swift */,
9092
156C174F1E0206BB00101575 /* Updater.swift */,
93+
156C175D1E0211AB00101575 /* Example.swift */,
9194
);
9295
path = MimeLibGenerator;
9396
sourceTree = "<group>";
@@ -151,6 +154,7 @@
151154
isa = PBXSourcesBuildPhase;
152155
buildActionMask = 2147483647;
153156
files = (
157+
156C175E1E0211AB00101575 /* Example.swift in Sources */,
154158
15FBD06A1E01F41200E75B63 /* String+Tools.swift in Sources */,
155159
15FBD0631E01F3CE00E75B63 /* main.swift in Sources */,
156160
156C17501E0206BB00101575 /* Updater.swift in Sources */,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Example.swift
3+
// MimeLibGenerator
4+
//
5+
// Created by Ondrej Rafaj on 14/12/2016.
6+
// Copyright © 2016 manGoweb UK Ltd. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
final class Example {
13+
14+
static func run() {
15+
print("Couple of examples:")
16+
17+
print(Mime.fileExtension(forMime: "text/plain")!) // Prints: txt
18+
print(Mime.fileExtension(forMime: "application/octet-stream")!) // Prints: bin
19+
print(Mime.get(fileExtension: "psd")!.rawValue) // Prints: image/vnd.adobe.photoshop
20+
print(Mime.get(fileExtension: "ogg") ?? "nil result") // Prints: nil result
21+
print(Mime.string(forPath: "/home/file.pdf")!) // Prints: application/pdf
22+
23+
print("\n\n")
24+
}
25+
}

Generator/MimeLibGenerator/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ else {
117117

118118
print("\nThank you for using MimeLibGenerator!\n\nOndrej Rafaj & team manGoweb UK! (http://www.mangoweb.cz/en)\n\n\n")
119119

120-
120+
Example.run()
121121

122122

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ If you want to generate files straight from the Xcode project, you have two opti
1919
.Package(url: "https://github.com/manGoweb/MimeLib.git", majorVersion: 1)
2020
```
2121

22+
## Usage
23+
24+
Couple of examples:
25+
```swift
26+
Mime.fileExtension(forMime: "text/plain") // Prints: txt
27+
Mime.fileExtension(forMime: "application/octet-stream") // Prints: bin
28+
Mime.get(fileExtension: "psd")!.rawValue // Prints: image/vnd.adobe.photoshop
29+
Mime.get(fileExtension: "ogg") ?? "nil result" // Prints: nil result
30+
Mime.string(forPath: "/home/file.pdf") // Prints: application/pdf
31+
```
32+
2233
## Info
2334

2435
:gift_heart: Contributing

Sources/Mime+Tools.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ import Foundation
1111

1212
public extension Mime {
1313

14-
public static func string(forUrl url: URL) -> String? {
14+
public static func get(fileUrl url: URL) -> MimeType? {
1515
let ext: String = url.pathExtension
1616
guard ext != "" else {
1717
return nil
1818
}
19-
return Mime.get(fileExtension: ext)?.rawValue
19+
return Mime.get(fileExtension: ext)
20+
}
21+
22+
public static func get(filePath path: String) -> MimeType? {
23+
let url: URL = URL(fileURLWithPath: path)
24+
return Mime.get(fileUrl: url)
25+
}
26+
27+
public static func string(forUrl url: URL) -> String? {
28+
return Mime.get(fileUrl: url)?.rawValue
2029
}
2130

2231
public static func string(forPath path: String) -> String? {

0 commit comments

Comments
 (0)