2015-01-08 20:23:49 +00:00
/*
Copyright 2014 OpenMarket Ltd
Licensed under the Apache License , Version 2.0 ( the " License " ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an " AS IS " BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
*/
# import <UIKit / UIKit.h>
2015-01-12 17:07:39 +00:00
// Provide the download progress
2015-01-08 20:23:49 +00:00
// object: URL
// userInfo: kMediaLoaderProgressRateKey : progress value nested in a NSNumber (range 0->1)
// : kMediaLoaderProgressStringKey : progress string XXX KB / XXX MB" (optional)
// : kMediaLoaderProgressRemaingTimeKey : remaining time string "XX s left" (optional)
// : kMediaLoaderProgressDownloadRateKey : string like XX MB/s (optional)
extern NSString * const kMediaDownloadProgressNotification ;
2015-01-12 17:07:39 +00:00
// Provide the upload progress
// object: uploadId
// userInfo: kMediaLoaderProgressRateKey : progress value nested in a NSNumber (range 0->1)
// : kMediaLoaderProgressStringKey : progress string XXX KB / XXX MB" (optional)
// : kMediaLoaderProgressRemaingTimeKey : remaining time string "XX s left" (optional)
// : kMediaLoaderProgressDownloadRateKey : string like XX MB/s (optional)
2015-01-08 20:23:49 +00:00
extern NSString * const kMediaUploadProgressNotification ;
2015-01-12 17:07:39 +00:00
2015-01-08 20:23:49 +00:00
// userInfo keys
extern NSString * const kMediaLoaderProgressRateKey ;
extern NSString * const kMediaLoaderProgressStringKey ;
extern NSString * const kMediaLoaderProgressRemaingTimeKey ;
extern NSString * const kMediaLoaderProgressDownloadRateKey ;
// The callback blocks
2015-01-12 17:07:39 +00:00
typedef void ( ^ blockMediaLoader_onSuccess ) ( NSString * url ) ; // url is a cache file path for successful download, or a remote url for upload.
2015-01-08 20:23:49 +00:00
typedef void ( ^ blockMediaLoader_onError ) ( NSError * error ) ;
@ interface MediaLoader : NSObject < NSURLConnectionDataDelegate > {
NSString * mimeType ;
2015-01-12 17:07:39 +00:00
blockMediaLoader_onSuccess onSuccess ;
2015-01-08 20:23:49 +00:00
blockMediaLoader_onError onError ;
2015-01-19 13:33:57 +00:00
NSString * folder ;
2015-01-12 17:07:39 +00:00
// Download
NSString * mediaURL ;
2015-01-08 20:23:49 +00:00
long long expectedSize ;
NSMutableData * downloadData ;
NSURLConnection * downloadConnection ;
// statistic info (bitrate, remaining time...)
CFAbsoluteTime statsStartTime ;
CFAbsoluteTime downloadStartTime ;
CFAbsoluteTime lastProgressEventTimeStamp ;
NSTimer * progressCheckTimer ;
2015-01-12 17:07:39 +00:00
// Upload
NSString * uploadId ;
CGFloat initialRange ;
CGFloat range ;
2015-01-19 14:30:45 +00:00
NSOperation * operation ;
2015-01-08 20:23:49 +00:00
}
2015-01-12 17:07:39 +00:00
@ property ( strong , readonly ) NSMutableDictionary * statisticsDict ;
2015-01-08 20:23:49 +00:00
2015-01-12 17:07:39 +00:00
- ( void ) cancel ;
// Download
- ( void ) downloadMedia : ( NSString * ) aMediaURL
2015-01-08 20:23:49 +00:00
mimeType : ( NSString * ) aMimeType
2015-01-19 13:33:57 +00:00
folder : ( NSString * ) folder
2015-01-12 17:07:39 +00:00
success : ( blockMediaLoader_onSuccess ) success
2015-01-08 20:23:49 +00:00
failure : ( blockMediaLoader_onError ) failure ;
2015-01-12 17:07:39 +00:00
// Upload
// initialRange / range: an upload could be a subpart of uploads. initialRange defines the global upload progress already did done before this current upload.
// range is the range value of this upload in the global scope.
// e.g. : Upload a media can be split in two parts :
// 1 - upload the thumbnail -> initialRange = 0, range = 0.1 : assume that the thumbnail upload is 10% of the upload process
// 2 - upload the media -> initialRange = 0.1, range = 0.9 : the media upload is 90% of the global upload
2015-01-19 13:33:57 +00:00
- ( id ) initWithUploadId : ( NSString * ) anUploadId initialRange : ( CGFloat ) anInitialRange andRange : ( CGFloat ) aRange folder : ( NSString * ) aFolder ;
2015-01-12 17:07:39 +00:00
- ( void ) uploadData : ( NSData * ) data
mimeType : ( NSString * ) aMimeType
success : ( blockMediaLoader_onSuccess ) success
failure : ( blockMediaLoader_onError ) failure ;
2015-01-08 20:23:49 +00:00
@ end