Skip to content

Commit 38fc2da

Browse files
author
Ben Pham
committed
create mimeTypeOverrides argument to override contentType
1 parent d0c4cb0 commit 38fc2da

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ declare module 'react-native-static-server' {
22
type Options = {
33
localOnly?: boolean
44
keepAlive?: boolean
5-
}
5+
mimeTypeOverrides?: Record<string, string>
6+
};
67

78
export default class StaticServer {
89
constructor(port: number, root?: string, opts?: Options)
@@ -13,10 +14,11 @@ declare module 'react-native-static-server' {
1314
keepAlive: boolean
1415
started: boolean
1516
_origin?: string
17+
mimeTypeOverrides: Record<string, string>
1618

1719
start: () => Promise<string>
1820
stop: () => Promise<any>
1921
isRunning: () => Promise<boolean>
2022
kill: () => void
2123
}
22-
}
24+
}

index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
NativeModules,
33
AppState,
44
Platform
5-
} from 'react-native';
5+
} from 'react-native';
66

77
const { FPStaticServer } = NativeModules;
88

@@ -18,37 +18,43 @@ class StaticServer {
1818
this.root = root || ROOT;
1919
this.localOnly = (opts && opts.localOnly) || false;
2020
this.keepAlive = (opts && opts.keepAlive) || false;
21+
this.mimeTypeOverrides = opts.mimeTypeOverrides || null;
2122
break;
2223
case 2:
2324
this.port = `${port}`;
24-
if (typeof(arguments[1]) === 'string') {
25+
if (typeof (arguments[1]) === 'string') {
2526
this.root = root;
2627
this.localOnly = false;
2728
this.keepAlive = false;
29+
this.mimeTypeOverrides = null;
2830
} else {
2931
this.root = ROOT;
3032
this.localOnly = (arguments[1] && arguments[1].localOnly) || false;
3133
this.keepAlive = (arguments[1] && arguments[1].keepAlive) || false;
34+
this.mimeTypeOverrides = arguments[1].mimeTypeOverrides || null;
3235
}
3336
break;
3437
case 1:
35-
if (typeof(arguments[0]) === 'number') {
38+
if (typeof (arguments[0]) === 'number') {
3639
this.port = `${port}`;
3740
this.root = ROOT;
3841
this.localOnly = false;
3942
this.keepAlive = false;
43+
this.mimeTypeOverrides = null;
4044
} else {
4145
this.port = PORT;
4246
this.root = ROOT;
4347
this.localOnly = (arguments[0] && arguments[0].localOnly) || false;
4448
this.keepAlive = (arguments[0] && arguments[0].keepAlive) || false;
49+
this.mimeTypeOverrides = arguments[0].mimeTypeOverrides || null;
4550
}
4651
break;
4752
default:
4853
this.port = PORT;
4954
this.root = ROOT;
5055
this.localOnly = false;
5156
this.keepAlive = false;
57+
this.mimeTypeOverrides = null;
5258
}
5359

5460

@@ -58,7 +64,7 @@ class StaticServer {
5864
}
5965

6066
start() {
61-
if( this.running ){
67+
if (this.running) {
6268
return Promise.resolve(this.origin);
6369
}
6470

@@ -69,7 +75,7 @@ class StaticServer {
6975
AppState.addEventListener('change', this._handleAppStateChangeFn);
7076
}
7177

72-
return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive)
78+
return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive, this.mimeTypeOverrides)
7379
.then((origin) => {
7480
this._origin = origin;
7581
return origin;

ios/FPStaticServer.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
#import "GCDWebServerFileResponse.h"
77
#import "GCDWebServerHTTPStatusCodes.h"
88

9-
@interface FPStaticServer : NSObject <RCTBridgeModule> {
10-
GCDWebServer* _webServer;
9+
@interface FPStaticServer : NSObject <RCTBridgeModule>
10+
{
11+
GCDWebServer *_webServer;
1112
}
1213

13-
@property(nonatomic, retain) NSString *localPath;
14-
@property(nonatomic, retain) NSString *url;
14+
@property(nonatomic, retain) NSString *localPath;
15+
@property(nonatomic, retain) NSString *url;
1516

16-
@property (nonatomic, retain) NSString* www_root;
17-
@property (nonatomic, retain) NSNumber* port;
18-
@property (assign) BOOL localhost_only;
19-
@property (assign) BOOL keep_alive;
17+
@property(nonatomic, retain) NSString *www_root;
18+
@property(nonatomic, retain) NSNumber *port;
19+
@property(nonatomic, retain) NSDictionary<NSString *, NSString *> *mime_type_overrides;
20+
@property(assign) BOOL localhost_only;
21+
@property(assign) BOOL keep_alive;
2022

2123
@end
22-

ios/FPStaticServer.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ - (dispatch_queue_t)methodQueue
3030
}
3131

3232

33-
RCT_EXPORT_METHOD(start: (NSString *)port
33+
RCT_EXPORT_METHOD(start:(NSString *)port
3434
root:(NSString *)optroot
3535
localOnly:(BOOL *)localhost_only
3636
keepAlive:(BOOL *)keep_alive
37+
mimeTypeOverrides:(NSDictionary *)mime_type_overrides
3738
resolver:(RCTPromiseResolveBlock)resolve
3839
rejecter:(RCTPromiseRejectBlock)reject) {
3940

@@ -67,12 +68,14 @@ - (dispatch_queue_t)methodQueue
6768

6869
self.localhost_only = localhost_only;
6970

71+
self.mime_type_overrides = mime_type_overrides;
72+
7073
if(_webServer.isRunning != NO) {
7174
NSLog(@"StaticServer already running at %@", self.url);
7275
resolve(self.url);
7376
return;
7477
}
75-
78+
7679
//[_webServer addGETHandlerForBasePath:@"/" directoryPath:self.www_root indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES];
7780
NSString *basePath = @"/";
7881
NSString *directoryPath = self.www_root;
@@ -104,11 +107,11 @@ - (dispatch_queue_t)methodQueue
104107
response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
105108
}
106109
} else if ([fileType isEqualToString:NSFileTypeRegular]) {
107-
if (allowRangeRequests) {
108-
response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
110+
if (allowRangeRequests) {
111+
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:request.byteRange isAttachment:NO mimeTypeOverrides:mime_type_overrides] ;
109112
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
110113
} else {
111-
response = [GCDWebServerFileResponse responseWithFile:filePath];
114+
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:NO mimeTypeOverrides:mime_type_overrides];
112115
}
113116
}
114117
}

0 commit comments

Comments
 (0)