Skip to content

Commit 2d64bdc

Browse files
committed
Added initial storage
1 parent f72b696 commit 2d64bdc

File tree

11 files changed

+231
-117
lines changed

11 files changed

+231
-117
lines changed

ios/Firestack.xcodeproj/project.pbxproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
/* Begin PBXBuildFile section */
1010
59F628FCB5C2B2900F31879C /* libPods-Firestack.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 58A186F11042C569425E5D5F /* libPods-Firestack.a */; };
1111
D950369E1D19C77400F7094D /* Firestack.m in Sources */ = {isa = PBXBuildFile; fileRef = D950369D1D19C77400F7094D /* Firestack.m */; };
12-
D96290331D6D145A0099A3EC /* FirestackAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = D96290321D6D145A0099A3EC /* FirestackAuth.m */; };
1312
D962903F1D6D15B00099A3EC /* FirestackErrors.m in Sources */ = {isa = PBXBuildFile; fileRef = D962903E1D6D15B00099A3EC /* FirestackErrors.m */; };
1413
D96290451D6D16100099A3EC /* FirestackAnalytics.m in Sources */ = {isa = PBXBuildFile; fileRef = D96290441D6D16100099A3EC /* FirestackAnalytics.m */; };
1514
D96290491D6D1D8C0099A3EC /* FirestackMessaging.m in Sources */ = {isa = PBXBuildFile; fileRef = D96290481D6D1D8C0099A3EC /* FirestackMessaging.m */; };
@@ -31,6 +30,8 @@
3130
D97261C51D551BF0000F703F /* libicucore.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D97261C41D551BF0000F703F /* libicucore.tbd */; };
3231
D97261C71D551BF5000F703F /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D97261C61D551BF5000F703F /* CFNetwork.framework */; };
3332
D97261C91D551BFF000F703F /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D97261C81D551BFF000F703F /* AddressBook.framework */; };
33+
D9D62E7C1D6D86FD003D826D /* FirestackStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = D9D62E7B1D6D86FD003D826D /* FirestackStorage.m */; };
34+
D9D62E801D6D8717003D826D /* FirestackAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = D9D62E7F1D6D8717003D826D /* FirestackAuth.m */; };
3435
/* End PBXBuildFile section */
3536

3637
/* Begin PBXContainerItemProxy section */
@@ -69,8 +70,6 @@
6970
D950369C1D19C77400F7094D /* Firestack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Firestack.h; path = Firestack/Firestack.h; sourceTree = "<group>"; };
7071
D950369D1D19C77400F7094D /* Firestack.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Firestack.m; path = Firestack/Firestack.m; sourceTree = "<group>"; };
7172
D95036B31D19C90300F7094D /* Pods.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Pods.xcodeproj; path = Pods/Pods.xcodeproj; sourceTree = "<group>"; };
72-
D96290311D6D145A0099A3EC /* FirestackAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirestackAuth.h; sourceTree = "<group>"; };
73-
D96290321D6D145A0099A3EC /* FirestackAuth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirestackAuth.m; sourceTree = "<group>"; };
7473
D96290391D6D152A0099A3EC /* FirestackEvents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FirestackEvents.h; path = Firestack/FirestackEvents.h; sourceTree = "<group>"; };
7574
D962903D1D6D15B00099A3EC /* FirestackErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FirestackErrors.h; path = Firestack/FirestackErrors.h; sourceTree = "<group>"; };
7675
D962903E1D6D15B00099A3EC /* FirestackErrors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FirestackErrors.m; path = Firestack/FirestackErrors.m; sourceTree = "<group>"; };
@@ -98,6 +97,10 @@
9897
D97261C61D551BF5000F703F /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
9998
D97261C81D551BFF000F703F /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; };
10099
D9CFBE441D531058002623BF /* buildScript.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = buildScript.sh; sourceTree = "<group>"; };
100+
D9D62E7A1D6D86FD003D826D /* FirestackStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FirestackStorage.h; path = Firestack/FirestackStorage.h; sourceTree = "<group>"; };
101+
D9D62E7B1D6D86FD003D826D /* FirestackStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FirestackStorage.m; path = Firestack/FirestackStorage.m; sourceTree = "<group>"; };
102+
D9D62E7E1D6D8717003D826D /* FirestackAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FirestackAuth.h; path = Firestack/FirestackAuth.h; sourceTree = "<group>"; };
103+
D9D62E7F1D6D8717003D826D /* FirestackAuth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FirestackAuth.m; path = Firestack/FirestackAuth.m; sourceTree = "<group>"; };
101104
EF9B4692F57E7CF2C25A04C2 /* Pods-Firestack.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestack.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestack/Pods-Firestack.debug.xcconfig"; sourceTree = "<group>"; };
102105
/* End PBXFileReference section */
103106

@@ -197,8 +200,8 @@
197200
D96290351D6D145F0099A3EC /* Modules */ = {
198201
isa = PBXGroup;
199202
children = (
200-
D96290311D6D145A0099A3EC /* FirestackAuth.h */,
201-
D96290321D6D145A0099A3EC /* FirestackAuth.m */,
203+
D9D62E7E1D6D8717003D826D /* FirestackAuth.h */,
204+
D9D62E7F1D6D8717003D826D /* FirestackAuth.m */,
202205
D96290391D6D152A0099A3EC /* FirestackEvents.h */,
203206
D962903D1D6D15B00099A3EC /* FirestackErrors.h */,
204207
D962903E1D6D15B00099A3EC /* FirestackErrors.m */,
@@ -208,6 +211,8 @@
208211
D96290481D6D1D8C0099A3EC /* FirestackMessaging.m */,
209212
D96290831D6D28B80099A3EC /* FirestackDatabase.h */,
210213
D96290841D6D28B80099A3EC /* FirestackDatabase.m */,
214+
D9D62E7A1D6D86FD003D826D /* FirestackStorage.h */,
215+
D9D62E7B1D6D86FD003D826D /* FirestackStorage.m */,
211216
);
212217
name = Modules;
213218
sourceTree = "<group>";
@@ -320,8 +325,9 @@
320325
isa = PBXSourcesBuildPhase;
321326
buildActionMask = 2147483647;
322327
files = (
328+
D9D62E801D6D8717003D826D /* FirestackAuth.m in Sources */,
323329
D96290451D6D16100099A3EC /* FirestackAnalytics.m in Sources */,
324-
D96290331D6D145A0099A3EC /* FirestackAuth.m in Sources */,
330+
D9D62E7C1D6D86FD003D826D /* FirestackStorage.m in Sources */,
325331
D962903F1D6D15B00099A3EC /* FirestackErrors.m in Sources */,
326332
D950369E1D19C77400F7094D /* Firestack.m in Sources */,
327333
D96290491D6D1D8C0099A3EC /* FirestackMessaging.m in Sources */,

ios/Firestack/Firestack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
FIRAuthStateDidChangeListenerHandle authListenerHandle;
1515
}
1616

17+
+ (Firestack *) getInstance;
1718
+ (void) registerForNotification:(NSString *) typeStr andToken:(NSData *)deviceToken;
1819

1920
@property (nonatomic) BOOL debug;

ios/Firestack/Firestack.m

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -110,98 +110,6 @@ @implementation Firestack
110110

111111
#pragma mark - Storage
112112

113-
- (NSString *) getStorageUrl
114-
{
115-
NSDictionary *cfg = [self getConfig];
116-
NSString *storageUrl = [NSString stringWithFormat:@"gs://%@", [cfg valueForKey:@"storageBucket"]];
117-
return storageUrl;
118-
}
119-
120-
RCT_EXPORT_METHOD(uploadFile:(NSString *) name
121-
path:(NSString *)path
122-
metadata:(NSDictionary *)metadata
123-
callback:(RCTResponseSenderBlock) callback)
124-
{
125-
NSString *urlStr = [self getStorageUrl];
126-
127-
if (urlStr == nil) {
128-
NSError *err = [[NSError alloc] init];
129-
[err setValue:@"Storage configuration error" forKey:@"name"];
130-
[err setValue:@"Call setStorageUrl() first" forKey:@"description"];
131-
return callback(@[err]);
132-
}
133-
134-
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:urlStr];
135-
FIRStorageReference *uploadRef = [storageRef child:name];
136-
137-
NSURL *localFile = [NSURL fileURLWithPath:path];
138-
139-
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata];
140-
141-
FIRStorageUploadTask *uploadTask = [uploadRef putFile:localFile
142-
metadata:firmetadata];
143-
// Listen for state changes, errors, and completion of the upload.
144-
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
145-
// Upload resumed, also fires when the upload starts
146-
[self sendJSEvent:@"uploadResumed" props:@{
147-
@"ref": snapshot.reference.bucket
148-
}];
149-
}];
150-
151-
[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
152-
// Upload paused
153-
[self sendJSEvent:@"uploadPaused" props:@{
154-
@"ref": snapshot.reference.bucket
155-
}];
156-
}];
157-
[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
158-
// Upload reported progress
159-
double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
160-
161-
[self sendJSEvent:@"uploadProgress" props:@{
162-
@"progress": @(percentComplete || 0.0)
163-
}];
164-
165-
}];
166-
167-
[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
168-
// Upload completed successfully
169-
FIRStorageReference *ref = snapshot.reference;
170-
NSDictionary *props = @{
171-
@"fullPath": ref.fullPath,
172-
@"bucket": ref.bucket,
173-
@"name": ref.name,
174-
@"metadata": [snapshot.metadata dictionaryRepresentation]
175-
};
176-
177-
callback(@[[NSNull null], props]);
178-
}];
179-
180-
[uploadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
181-
if (snapshot.error != nil) {
182-
NSError *err = [[NSError alloc] init];
183-
switch (snapshot.error.code) {
184-
case FIRStorageErrorCodeObjectNotFound:
185-
// File doesn't exist
186-
[err setValue:@"File does not exist" forKey:@"description"];
187-
break;
188-
case FIRStorageErrorCodeUnauthorized:
189-
// User doesn't have permission to access file
190-
[err setValue:@"You do not have permissions" forKey:@"description"];
191-
break;
192-
case FIRStorageErrorCodeCancelled:
193-
// User canceled the upload
194-
[err setValue:@"Upload cancelled" forKey:@"description"];
195-
break;
196-
case FIRStorageErrorCodeUnknown:
197-
// Unknown error occurred, inspect the server response
198-
[err setValue:@"Unknown error" forKey:@"description"];
199-
break;
200-
}
201-
202-
callback(@[err]);
203-
}}];
204-
}
205113

206114
#pragma mark RemoteConfig
207115

ios/Firestack/FirestackEvents.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ static NSString *const DATABASE_CHILD_ADDED_EVENT = @"child_added";
1515
static NSString *const DATABASE_CHILD_MODIFIED_EVENT = @"child_changed";
1616
static NSString *const DATABASE_CHILD_REMOVED_EVENT = @"child_removed";
1717
static NSString *const DATABASE_CHILD_MOVED_EVENT = @"child_moved";
18-
static NSString *const DATABASE_ERROR_EVENT = @"database_error";
18+
static NSString *const DATABASE_ERROR_EVENT = @"database_error";
19+
20+
// Storage
21+
static NSString *const STORAGE_UPLOAD_PROGRESS = @"upload_progress";
22+
static NSString *const STORAGE_UPLOAD_PAUSED = @"upload_paused";
23+
static NSString *const STORAGE_UPLOAD_RESUMED = @"upload_resumed";

ios/Firestack/FirestackStorage.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// FirestackStorage.h
3+
// Firestack
4+
//
5+
// Created by Ari Lerner on 8/24/16.
6+
// Copyright © 2016 Facebook. All rights reserved.
7+
//
8+
9+
#import "RCTBridgeModule.h"
10+
#import "RCTEventEmitter.h"
11+
12+
@interface FirestackStorage : RCTEventEmitter <RCTBridgeModule> {
13+
14+
}
15+
16+
@property (nonatomic) NSString *_storageUrl;
17+
18+
@end

ios/Firestack/FirestackStorage.m

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// FirestackStorage.m
3+
// Firestack
4+
//
5+
// Created by Ari Lerner on 8/24/16.
6+
// Copyright © 2016 Facebook. All rights reserved.
7+
//
8+
9+
#import "FirestackStorage.h"
10+
#import "FirestackEvents.h"
11+
12+
@import FirebaseStorage;
13+
14+
@implementation FirestackStorage
15+
16+
RCT_EXPORT_MODULE(FirestackStorage);
17+
18+
- (void) setStorageUrl:(NSString *)storageBucket
19+
{
20+
NSString *storageUrl = [NSString stringWithFormat:@"gs://%@", storageBucket];
21+
self._storageUrl = storageUrl;
22+
}
23+
24+
//- (NSString *) getStorageUrl
25+
//{
26+
// NSDictionary *cfg = [self getConfig];
27+
// NSString *storageUrl = [NSString stringWithFormat:@"gs://%@", [cfg valueForKey:@"storageBucket"]];
28+
// return storageUrl;
29+
//}
30+
31+
RCT_EXPORT_METHOD(uploadFile:(NSString *) name
32+
path:(NSString *)path
33+
metadata:(NSDictionary *)metadata
34+
callback:(RCTResponseSenderBlock) callback)
35+
{
36+
NSString *urlStr = self._storageUrl;
37+
38+
if (urlStr == nil) {
39+
NSError *err = [[NSError alloc] init];
40+
[err setValue:@"Storage configuration error" forKey:@"name"];
41+
[err setValue:@"Call setStorageUrl() first" forKey:@"description"];
42+
return callback(@[err]);
43+
}
44+
45+
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:urlStr];
46+
FIRStorageReference *uploadRef = [storageRef child:name];
47+
48+
NSURL *localFile = [NSURL fileURLWithPath:path];
49+
50+
FIRStorageMetadata *firmetadata = [[FIRStorageMetadata alloc] initWithDictionary:metadata];
51+
52+
FIRStorageUploadTask *uploadTask = [uploadRef putFile:localFile
53+
metadata:firmetadata];
54+
// Listen for state changes, errors, and completion of the upload.
55+
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
56+
// Upload resumed, also fires when the upload starts
57+
[self sendJSEvent:STORAGE_UPLOAD_RESUMED props:@{
58+
@"ref": snapshot.reference.bucket
59+
}];
60+
}];
61+
62+
[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
63+
// Upload paused
64+
[self sendJSEvent:STORAGE_UPLOAD_PAUSED props:@{
65+
@"ref": snapshot.reference.bucket
66+
}];
67+
}];
68+
[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
69+
// Upload reported progress
70+
double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
71+
72+
[self sendJSEvent:STORAGE_UPLOAD_PROGRESS props:@{
73+
@"progress": @(percentComplete || 0.0)
74+
}];
75+
76+
}];
77+
78+
[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
79+
// Upload completed successfully
80+
FIRStorageReference *ref = snapshot.reference;
81+
NSDictionary *props = @{
82+
@"fullPath": ref.fullPath,
83+
@"bucket": ref.bucket,
84+
@"name": ref.name,
85+
@"metadata": [snapshot.metadata dictionaryRepresentation]
86+
};
87+
88+
callback(@[[NSNull null], props]);
89+
}];
90+
91+
[uploadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
92+
if (snapshot.error != nil) {
93+
NSError *err = [[NSError alloc] init];
94+
switch (snapshot.error.code) {
95+
case FIRStorageErrorCodeObjectNotFound:
96+
// File doesn't exist
97+
[err setValue:@"File does not exist" forKey:@"description"];
98+
break;
99+
case FIRStorageErrorCodeUnauthorized:
100+
// User doesn't have permission to access file
101+
[err setValue:@"You do not have permissions" forKey:@"description"];
102+
break;
103+
case FIRStorageErrorCodeCancelled:
104+
// User canceled the upload
105+
[err setValue:@"Upload cancelled" forKey:@"description"];
106+
break;
107+
case FIRStorageErrorCodeUnknown:
108+
// Unknown error occurred, inspect the server response
109+
[err setValue:@"Unknown error" forKey:@"description"];
110+
break;
111+
}
112+
113+
callback(@[err]);
114+
}}];
115+
}
116+
117+
// Not sure how to get away from this... yet
118+
- (NSArray<NSString *> *)supportedEvents {
119+
return @[
120+
STORAGE_UPLOAD_PAUSED,
121+
STORAGE_UPLOAD_RESUMED,
122+
STORAGE_UPLOAD_PROGRESS
123+
];
124+
}
125+
126+
- (void) sendJSEvent:(NSString *)title
127+
props:(NSDictionary *)props
128+
{
129+
@try {
130+
[self sendEventWithName:title
131+
body:props];
132+
}
133+
@catch (NSException *err) {
134+
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);
135+
}
136+
}
137+
138+
139+
@end

lib/firestack.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import promisify from './promisify'
1919
import RemoteConfig from './modules/remoteConfig'
2020
import {Authentication} from './modules/authentication'
2121
import {Database} from './modules/database'
22+
import {Analytics} from './modules/analytics'
23+
import {Storage} from './modules/storage'
2224

2325
let log;
2426
export class Firestack {
@@ -89,6 +91,12 @@ export class Firestack {
8991
return this._analytics;
9092
}
9193

94+
// storage
95+
get storage() {
96+
if (!this._storage) { this._storage = new Storage(this); }
97+
return this._storage;
98+
}
99+
92100
// Storage
93101
//
94102
// /**
@@ -100,24 +108,7 @@ export class Firestack {
100108
// return promisify('setStorageUrl')(url);
101109
// }
102110

103-
/**
104-
* Upload a filepath
105-
* @param {string} name The destination for the file
106-
* @param {string} filepath The local path of the file
107-
* @param {object} metadata An object containing metadata
108-
* @return {Promise}
109-
*/
110-
uploadFile(name, filepath, metadata) {
111-
return promisify('uploadFile')(name, filepath, metadata);
112-
}
113-
114-
/**
115-
* The native storage object provided by Firebase
116-
* @return {instance}
117-
*/
118-
get storage() {
119-
return storage();
120-
}
111+
121112

122113
// other
123114
get ServerValue() {

0 commit comments

Comments
 (0)