Skip to content

Commit

Permalink
feat(ios): add API for progressive upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Mar 28, 2024
1 parent bd1c95c commit d780032
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ios/RNUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,28 @@ @interface RCT_EXTERN_REMAP_MODULE(ApiVideoUploader, RNUploader, NSObject<RCTBri

RCT_EXTERN_METHOD(setTimeout:(NSNumber)timeout)

RCT_EXTERN_METHOD(uploadWithUploadToken:(NSString)token:(NSString)filePath:(NSString)videoId
// MARK: Regular upload
RCT_EXTERN_METHOD(uploadWithUploadToken:(nonnull NSString)token:(nonnull NSString)filePath:(NSString)videoId
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(upload:(NSString)videoId:(NSString)filePath
RCT_EXTERN_METHOD(upload:(nonnull NSString)videoId:(nonnull NSString)filePath
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

// MARK: Progressive upload
RCT_EXTERN_METHOD(createUploadProgressiveSession:(nonnull NSString)sessionId:(nonnull NSString)videoId)
RCT_EXTERN_METHOD(createProgressiveUploadWithUploadTokenSession:(nonnull NSString)sessionId:(nonnull NSString)token:(NSString)videoId)

RCT_EXTERN_METHOD(uploadPart:(nonnull NSString)sessionId:(nonnull NSString)filePath
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(uploadLastPart:(nonnull NSString)sessionId:(nonnull NSString)filePath
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(disposeProgressiveUploadSession:(nonnull NSString)sessionId)

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
Expand Down
41 changes: 41 additions & 0 deletions ios/RNUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,45 @@ class RNUploader: NSObject {
reject("upload_failed", error.localizedDescription, error)
}
}

@objc(createUploadProgressiveSession::)
func createUploadProgressiveSession(sessionId: String, videoId: String) {
do {
try uploadModule.createUploadProgressiveSession(sessionId: sessionId, videoId: videoId)
} catch {
fatalError("Failed to create progressive upload session: \(error)")
}
}

@objc(createProgressiveUploadWithUploadTokenSession:::)
func createProgressiveUploadWithUploadTokenSession(sessionId: String, token: String, videoId: String?) {
do {
try uploadModule.createProgressiveUploadWithUploadTokenSession(sessionId: sessionId, token: token, videoId: videoId)
} catch {
fatalError("Failed to create progressive upload with upload token session: \(error)")
}
}

@objc(uploadPart::withResolver:withRejecter:)
func uploadPart(sessionId: String, filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
uploadModule.uploadPart(sessionId: sessionId, filePath: filePath, onProgress: { _ in }, onSuccess: { _ in
resolve(nil)
}, onError: { error in
reject("upload_part_failed", error.localizedDescription, error)
})
}

@objc(uploadLastPart::withResolver:withRejecter:)
func uploadLastPart(sessionId: String, filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
uploadModule.uploadLastPart(sessionId: sessionId, filePath: filePath, onProgress: { _ in }, onSuccess: { _ in
resolve(nil)
}, onError: { error in
reject("upload_part_failed", error.localizedDescription, error)
})
}

@objc(disposeProgressiveUploadSession:)
func disposeProgressiveUploadSession(sessionId: String) {
uploadModule.disposeProgressiveUploadSession(sessionId)
}
}

0 comments on commit d780032

Please sign in to comment.