Commit 459ac07e5137766c8ff4d596f8624273c57b4eb5
1 parent
7c9719e4
提交上传代码
Showing
3 changed files
with
95 additions
and
40 deletions
CNLiveUploadManager.podspec
CNLiveUploadManager/Classes/CNLiveUploadManager.h
| ... | ... | @@ -37,16 +37,16 @@ |
| 37 | 37 | //////////////////////////////////////////////////////// |
| 38 | 38 | //////////// 发送图片,视频限制 |
| 39 | 39 | //////////////////////////////////////////////////////// |
| 40 | -#define kFriendsCircle_limit_photo 20*1024.0*1024.0 //朋友圈限制图片20M | |
| 41 | -#define kFirendsCircle_limit_video 30*1024.0*1024.0 //朋友圈限制视频30M | |
| 42 | -#define kTarentoComment_limit_photo 10*1024.0*1024.0 //达人评论10M | |
| 43 | -#define kSweepCode_limit_phoho 10*1024.0*1024.0 //扫码10M限制 | |
| 44 | -#define kChat_limit_photo 20*1024.0*1024.0 //聊天限制图片20M | |
| 45 | -#define kChat_limit_video 20*1024.0*1024.0 //聊天限制视频20M | |
| 46 | -#define kChatBackgroupImage_limit_photo 5*1024.0*1024.0 //聊天背景图限制5 M | |
| 40 | +#define kFriendsCircle_limit_photo_upload 20*1024.0*1024.0 //朋友圈限制图片20M | |
| 41 | +#define kFirendsCircle_limit_video_upload 30*1024.0*1024.0 //朋友圈限制视频30M | |
| 42 | +#define kTarentoComment_limit_photo_upload 10*1024.0*1024.0 //达人评论10M | |
| 43 | +#define kSweepCode_limit_phoho_upload 10*1024.0*1024.0 //扫码10M限制 | |
| 44 | +#define kChat_limit_photo_upload 20*1024.0*1024.0 //聊天限制图片20M | |
| 45 | +#define kChat_limit_video_upload 20*1024.0*1024.0 //聊天限制视频20M | |
| 46 | +#define kChatBackgroupImage_limit_photo_upload 5*1024.0*1024.0 //聊天背景图限制5 M | |
| 47 | 47 | #define kChat_limit_video_length 5*60 //聊天限制视频5分钟 |
| 48 | -#define kFriendsCircle_limit_video_maxEdit 5*60 //朋友圈限制视频5分钟 | |
| 49 | -#define kFriendsCircle_limit_video_max 10*60 //朋友圈限制最大视频编辑10分钟 | |
| 48 | +#define kFriendsCircle_limit_video_maxEdit_upload 5*60 //朋友圈限制视频5分钟 | |
| 49 | +#define kFriendsCircle_limit_video_max_upload 10*60 //朋友圈限制最大视频编辑10分钟 | |
| 50 | 50 | |
| 51 | 51 | typedef NS_ENUM(NSUInteger, CNLiveUploadType) { |
| 52 | 52 | CNLiveUploadTypeQN,//七牛 |
| ... | ... | @@ -85,6 +85,7 @@ typedef void (^progressHandlerBlock)(float progress);//进度 |
| 85 | 85 | */ |
| 86 | 86 | +(CNLiveUploadManager *)shareInsance; |
| 87 | 87 | |
| 88 | +#pragma mark - 业务层 | |
| 88 | 89 | /// 单张上传NSData |
| 89 | 90 | + (void)uploadType:(CNLiveUploadType)type token:(NSString*)token data:(NSData *)data progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure; |
| 90 | 91 | |
| ... | ... | @@ -97,6 +98,8 @@ typedef void (^progressHandlerBlock)(float progress);//进度 |
| 97 | 98 | /// (视频)路径上传 |
| 98 | 99 | + (void)uploadVideoType:(CNLiveUploadType)type file:(NSString *)file token:(NSString *)token progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure; |
| 99 | 100 | |
| 101 | +#pragma mark - 业务层 + 限制 | |
| 102 | + | |
| 100 | 103 | /// 单图上传 (asset取得Image - 压缩或原图 - 限制 - 筛选) |
| 101 | 104 | + (void)uploadLimitType:(CNLiveUploadType)type phAsset:(PHAsset *)phAsset isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType token:(NSString *)token progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure; |
| 102 | 105 | |
| ... | ... | @@ -110,8 +113,11 @@ typedef void (^progressHandlerBlock)(float progress);//进度 |
| 110 | 113 | + (void)uploadLimitType:(CNLiveUploadType)type token:(NSString *)token phAssetArray:(NSMutableArray<PHAsset *> *)phAssetArray isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType progress:(progressHandlerBlock)progress success:(successBlock)success failure:(failureBlock)failure; |
| 111 | 114 | |
| 112 | 115 | /// 单个视频系统视频上传 (video - 压缩 - 限制 - 筛选) |
| 116 | +/// outputPath = [CNLiveBusinessTools newChatFilePathByUserId:self.conversationId] || [CNLiveBusinessTools chatFilePathByUserId:CNUserShareModel.uid] | |
| 113 | 117 | + (void)uploadLimitVideoType:(CNLiveUploadType)type token:(NSString *)token asset:(id)asset limitType:(AppPhototLimitType)limitType outputPath:(NSString *)outputPath progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure ; |
| 114 | 118 | |
| 119 | +#pragma mark - 业务层 + 限制 + token | |
| 120 | + | |
| 115 | 121 | /// 请求token 单张上传 (asset - 压缩 - 限制 - 筛选) |
| 116 | 122 | + (void)uploadRequestType:(CNLiveUploadType)type url:(NSString *)url dic:(NSDictionary *)dic phAsset:(id)phAsset isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure; |
| 117 | 123 | |
| ... | ... | @@ -125,6 +131,7 @@ typedef void (^progressHandlerBlock)(float progress);//进度 |
| 125 | 131 | + (void)uploadRequestType:(CNLiveUploadType)type url:(NSString *)url dic:(NSDictionary *)dic phAssetArray:(NSMutableArray<PHAsset *> *)phAssetArray isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType progress:(progressHandlerBlock)progress success:(successBlock)success failure:(failureBlock)failure; |
| 126 | 132 | |
| 127 | 133 | /// 请求token 单个视频上传 (video - 压缩 - 限制 - 筛选) |
| 134 | +/// outputPath = [CNLiveBusinessTools newChatFilePathByUserId:self.conversationId] || [CNLiveBusinessTools chatFilePathByUserId:CNUserShareModel.uid] | |
| 128 | 135 | + (void)uploadRequestVideoType:(CNLiveUploadType)type url:(NSString *)url dic:(NSDictionary *)dic asset:(id)asset limitType:(AppPhototLimitType)limitType outputPath:(NSString *)outputPath progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure; |
| 129 | 136 | |
| 130 | 137 | /// 请求token 单个视频上传 (video - 七牛短视频压缩 - 限制 - 筛选) | ... | ... |
CNLiveUploadManager/Classes/CNLiveUploadManager.m
| ... | ... | @@ -513,10 +513,28 @@ static CNLiveUploadManager *_uploadManager; |
| 513 | 513 | /// 单张NSData上传 - Key |
| 514 | 514 | -(void)qnUploadWithToken:(NSString*)token data:(NSData *)data progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure{ |
| 515 | 515 | |
| 516 | + //七牛token | |
| 517 | + if ([NSString isEmpty:token]) { | |
| 518 | + NSError *error = [NSError errorWithDomain:@"七牛token为空" code:100 userInfo:@{NSLocalizedDescriptionKey:@"七牛token为空,请重新获取"}]; | |
| 519 | + !failure ? : failure(nil,error); | |
| 520 | + return; | |
| 521 | + } | |
| 522 | + | |
| 523 | + if (!data) { | |
| 524 | + NSError *error = [NSError errorWithDomain:@"数据为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"数据为空,请重新获取"}]; | |
| 525 | + !failure ? : failure(nil,error); | |
| 526 | + return; | |
| 527 | + } | |
| 516 | 528 | //初始化上传需要的参数 |
| 517 | 529 | //imageKey获取x |
| 518 | 530 | NSString *key = [NSString getImageBuketkeyFromServerWithExt:[data imageFormatType]]; |
| 519 | 531 | |
| 532 | + if (!key) { | |
| 533 | + NSError *error = [NSError errorWithDomain:@"数据key为空" code:102 userInfo:@{NSLocalizedDescriptionKey:@"数据key为空,请重新获取"}]; | |
| 534 | + !failure ? : failure(nil,error); | |
| 535 | + return; | |
| 536 | + } | |
| 537 | + | |
| 520 | 538 | [self qnUploadWithToken:token data:data key:key progress:progress success:success failure:failure]; |
| 521 | 539 | |
| 522 | 540 | } |
| ... | ... | @@ -524,12 +542,38 @@ static CNLiveUploadManager *_uploadManager; |
| 524 | 542 | /// 单张上传 - Key - 压缩 |
| 525 | 543 | -(void)qnUploadWithToken:(NSString*)token image:(UIImage *)image progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure{ |
| 526 | 544 | |
| 545 | + //七牛token | |
| 546 | + if ([NSString isEmpty:token]) { | |
| 547 | + NSError *error = [NSError errorWithDomain:@"七牛token为空" code:100 userInfo:@{NSLocalizedDescriptionKey:@"七牛token为空,请重新获取"}]; | |
| 548 | + !failure ? : failure(nil,error); | |
| 549 | + return; | |
| 550 | + } | |
| 551 | + | |
| 552 | + if (!image) { | |
| 553 | + NSError *error = [NSError errorWithDomain:@"数据为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"数据为空,请重新获取"}]; | |
| 554 | + !failure ? : failure(nil,error); | |
| 555 | + return; | |
| 556 | + } | |
| 557 | + | |
| 527 | 558 | //初始化上传需要的参数 |
| 528 | 559 | //imageData转换,压缩 |
| 529 | 560 | NSData *data = [UIImage zipNSDataWithImage:image]; |
| 561 | + | |
| 562 | + if (!data) { | |
| 563 | + NSError *error = [NSError errorWithDomain:@"数据为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"数据为空,请重新获取"}]; | |
| 564 | + !failure ? : failure(nil,error); | |
| 565 | + return; | |
| 566 | + } | |
| 567 | + | |
| 530 | 568 | //imageKey获取 |
| 531 | 569 | NSString *key = [NSString getImageBuketkeyFromServerWithExt:[data imageFormatType]]; |
| 532 | 570 | |
| 571 | + if (!key) { | |
| 572 | + NSError *error = [NSError errorWithDomain:@"数据key为空" code:102 userInfo:@{NSLocalizedDescriptionKey:@"数据key为空,请重新获取"}]; | |
| 573 | + !failure ? : failure(nil,error); | |
| 574 | + return; | |
| 575 | + } | |
| 576 | + | |
| 533 | 577 | [self qnUploadWithToken:token data:data key:key progress:progress success:success failure:failure]; |
| 534 | 578 | |
| 535 | 579 | } |
| ... | ... | @@ -585,6 +629,11 @@ static CNLiveUploadManager *_uploadManager; |
| 585 | 629 | for (int i = 0; i < dataArray.count; i++) { |
| 586 | 630 | |
| 587 | 631 | NSString *key = [NSString getImageBuketkeyFromServerWithExt:[dataArray[i] imageFormatType]]; |
| 632 | + if (!key) { | |
| 633 | + NSError *error = [NSError errorWithDomain:@"数据key为空" code:102 userInfo:@{NSLocalizedDescriptionKey:@"数据key为空,请重新获取"}]; | |
| 634 | + !failure ? : failure(nil,error); | |
| 635 | + return; | |
| 636 | + } | |
| 588 | 637 | [keyArray addObject:key]; |
| 589 | 638 | } |
| 590 | 639 | |
| ... | ... | @@ -620,6 +669,12 @@ static CNLiveUploadManager *_uploadManager; |
| 620 | 669 | NSArray *extArr = [file componentsSeparatedByString:@"."]; |
| 621 | 670 | NSString *key = [NSString getBucketKeyFromServerWithExt:[extArr lastObject]]; |
| 622 | 671 | |
| 672 | + if (!key) { | |
| 673 | + NSError *error = [NSError errorWithDomain:@"数据key为空" code:102 userInfo:@{NSLocalizedDescriptionKey:@"数据key为空,请重新获取"}]; | |
| 674 | + !failure ? : failure(nil,error); | |
| 675 | + return; | |
| 676 | + } | |
| 677 | + | |
| 623 | 678 | [self qnUploadVideoWithToken:token file:file key:key progress:progress success:success failure:failure]; |
| 624 | 679 | } |
| 625 | 680 | |
| ... | ... | @@ -968,29 +1023,23 @@ static CNLiveUploadManager *_uploadManager; |
| 968 | 1023 | |
| 969 | 1024 | //七牛token |
| 970 | 1025 | if ([NSString isEmpty:token]) { |
| 971 | - dispatch_async(dispatch_get_main_queue(), ^{ | |
| 972 | - NSError *error = [NSError errorWithDomain:@"七牛token为空" code:100 userInfo:@{NSLocalizedDescriptionKey:@"七牛token为空,请重新获取"}]; | |
| 973 | - !failure ? : failure(nil,error); | |
| 974 | - return; | |
| 975 | - }); | |
| 1026 | + NSError *error = [NSError errorWithDomain:@"七牛token为空" code:100 userInfo:@{NSLocalizedDescriptionKey:@"七牛token为空,请重新获取"}]; | |
| 1027 | + !failure ? : failure(nil,error); | |
| 1028 | + return; | |
| 976 | 1029 | } |
| 977 | 1030 | |
| 978 | 1031 | //上传路径 |
| 979 | 1032 | if ([NSString isEmpty:outputPath]) { |
| 980 | - dispatch_async(dispatch_get_main_queue(), ^{ | |
| 981 | - NSError *error = [NSError errorWithDomain:@"上传路径为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"上传为空,请重新获取"}]; | |
| 982 | - !failure ? : failure(nil,error); | |
| 983 | - return; | |
| 984 | - }); | |
| 1033 | + NSError *error = [NSError errorWithDomain:@"上传路径为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"上传为空,请重新获取"}]; | |
| 1034 | + !failure ? : failure(nil,error); | |
| 1035 | + return; | |
| 985 | 1036 | |
| 986 | 1037 | } |
| 987 | 1038 | |
| 988 | 1039 | if (!asset) { |
| 989 | - dispatch_async(dispatch_get_main_queue(), ^{ | |
| 990 | - NSError *error = [NSError errorWithDomain:@"上传内容为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"上传内容为空,请重新获取"}]; | |
| 991 | - !failure ? : failure(nil,error); | |
| 992 | - return; | |
| 993 | - }); | |
| 1040 | + NSError *error = [NSError errorWithDomain:@"上传内容为空" code:101 userInfo:@{NSLocalizedDescriptionKey:@"上传内容为空,请重新获取"}]; | |
| 1041 | + !failure ? : failure(nil,error); | |
| 1042 | + return; | |
| 994 | 1043 | |
| 995 | 1044 | } |
| 996 | 1045 | |
| ... | ... | @@ -1445,7 +1494,7 @@ static CNLiveUploadManager *_uploadManager; |
| 1445 | 1494 | } |
| 1446 | 1495 | |
| 1447 | 1496 | |
| 1448 | -#pragma mark - 业务层_1 | |
| 1497 | +#pragma mark - 业务层 | |
| 1449 | 1498 | /// 单张上传NSData |
| 1450 | 1499 | + (void)uploadType:(CNLiveUploadType)type token:(NSString*)token data:(NSData *)data progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure { |
| 1451 | 1500 | if (type == CNLiveUploadTypeQN) { //七牛上传 |
| ... | ... | @@ -1495,7 +1544,7 @@ static CNLiveUploadManager *_uploadManager; |
| 1495 | 1544 | |
| 1496 | 1545 | } |
| 1497 | 1546 | |
| 1498 | -#pragma mark - 业务部分 | |
| 1547 | +#pragma mark - 业务层+限制 | |
| 1499 | 1548 | |
| 1500 | 1549 | /// 单图上传 (asset取得Image - 压缩或原图 - 限制 - 筛选) |
| 1501 | 1550 | + (void)uploadLimitType:(CNLiveUploadType)type phAsset:(PHAsset *)phAsset isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType token:(NSString *)token progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure { |
| ... | ... | @@ -1578,7 +1627,7 @@ static CNLiveUploadManager *_uploadManager; |
| 1578 | 1627 | |
| 1579 | 1628 | } |
| 1580 | 1629 | |
| 1581 | -#pragma mark - 业务_请求token + 上传 | |
| 1630 | +#pragma mark - 业务层 + 限制 + token | |
| 1582 | 1631 | /// 请求token 单张上传 (asset - 压缩 - 限制 - 筛选) |
| 1583 | 1632 | + (void)uploadRequestType:(CNLiveUploadType)type url:(NSString *)url dic:(NSDictionary *)dic phAsset:(id)phAsset isOriginal:(BOOL) isOriginal limitType:(AppPhototLimitType)limitType progress:(progressHandlerBlock)progress success:(successModelBlock)success failure:(failureModelBlock)failure{ |
| 1584 | 1633 | |
| ... | ... | @@ -1751,11 +1800,9 @@ static CNLiveUploadManager *_uploadManager; |
| 1751 | 1800 | [[CNLiveUploadManager shareInsance] uploadRequestPLShortVideoLimitWithToken:token asset:asset outputPath:outputPath limitType:limitType transcode: transcode progress:progress success:success failure:failure]; |
| 1752 | 1801 | |
| 1753 | 1802 | } else {//请求token失败 |
| 1754 | - dispatch_async(dispatch_get_main_queue(), ^{ | |
| 1755 | -// NSError *error = [NSError errorWithDomain:@"请求token失败" code:100 userInfo:@{NSLocalizedDescriptionKey:@"请求token失败,请重新获取"}]; | |
| 1756 | - !failure ? : failure(nil,error); | |
| 1757 | - return; | |
| 1758 | - }); | |
| 1803 | + NSError *error = [NSError errorWithDomain:@"请求token失败" code:100 userInfo:@{NSLocalizedDescriptionKey:@"请求token失败,请重新获取"}]; | |
| 1804 | + !failure ? : failure(nil,error); | |
| 1805 | + return; | |
| 1759 | 1806 | } |
| 1760 | 1807 | |
| 1761 | 1808 | }]; |
| ... | ... | @@ -1774,14 +1821,14 @@ static CNLiveUploadManager *_uploadManager; |
| 1774 | 1821 | - (BOOL)isVideoLimitType:(AppPhototLimitType)limitType data:(NSData *)data{ |
| 1775 | 1822 | switch (limitType) { |
| 1776 | 1823 | case AppPhototLimitTypeChat://聊天视频 |
| 1777 | - if (data.length <= kChat_limit_video) { | |
| 1824 | + if (data.length <= kChat_limit_video_upload) { | |
| 1778 | 1825 | return YES; |
| 1779 | 1826 | }else{ |
| 1780 | 1827 | return NO; |
| 1781 | 1828 | } |
| 1782 | 1829 | break; |
| 1783 | 1830 | case AppPhototLimitTypeFriendsCircle://朋友圈 |
| 1784 | - if (data.length <= kFirendsCircle_limit_video) { | |
| 1831 | + if (data.length <= kFirendsCircle_limit_video_upload) { | |
| 1785 | 1832 | return YES; |
| 1786 | 1833 | }else{ |
| 1787 | 1834 | return NO; |
| ... | ... | @@ -1803,28 +1850,28 @@ static CNLiveUploadManager *_uploadManager; |
| 1803 | 1850 | |
| 1804 | 1851 | switch (limitType) { |
| 1805 | 1852 | case AppPhototLimitTypeChat://聊天 |
| 1806 | - if (data.length <= kChat_limit_photo) { | |
| 1853 | + if (data.length <= kChat_limit_photo_upload) { | |
| 1807 | 1854 | return YES; |
| 1808 | 1855 | }else{ |
| 1809 | 1856 | return NO; |
| 1810 | 1857 | } |
| 1811 | 1858 | break; |
| 1812 | 1859 | case AppPhototLimitTypeFriendsCircle://朋友圈 |
| 1813 | - if (data.length <= kFriendsCircle_limit_photo) { | |
| 1860 | + if (data.length <= kFriendsCircle_limit_photo_upload) { | |
| 1814 | 1861 | return YES; |
| 1815 | 1862 | }else{ |
| 1816 | 1863 | return NO; |
| 1817 | 1864 | } |
| 1818 | 1865 | break; |
| 1819 | 1866 | case AppPhototLimitTypeSweepCode://扫码 |
| 1820 | - if (data.length <= kSweepCode_limit_phoho) { | |
| 1867 | + if (data.length <= kSweepCode_limit_phoho_upload) { | |
| 1821 | 1868 | return YES; |
| 1822 | 1869 | }else{ |
| 1823 | 1870 | return NO; |
| 1824 | 1871 | } |
| 1825 | 1872 | break; |
| 1826 | 1873 | case AppPhototLimitTypeTarentoComment://评论 |
| 1827 | - if (data.length <= kTarentoComment_limit_photo) { | |
| 1874 | + if (data.length <= kTarentoComment_limit_photo_upload) { | |
| 1828 | 1875 | return YES; |
| 1829 | 1876 | }else{ |
| 1830 | 1877 | return NO; |
| ... | ... | @@ -2001,9 +2048,10 @@ static CNLiveUploadManager *_uploadManager; |
| 2001 | 2048 | NSDateFormatter *formater = [[NSDateFormatter alloc] init]; |
| 2002 | 2049 | [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"]; |
| 2003 | 2050 | NSString *filaName = [NSString stringWithFormat:@"output-%@_%d_fileType=videoType.mp4",[formater stringFromDate:[NSDate date]],arc4random()]; |
| 2051 | + | |
| 2004 | 2052 | // NSString *outputPath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/output-%@.mp4", [formater stringFromDate:[NSDate date]]]; |
| 2005 | 2053 | NSString *editOutputPath = @""; |
| 2006 | - editOutputPath = [CNLiveUploadManager createVideoFolderOutputPath:outputPath]; | |
| 2054 | + editOutputPath = [[CNLiveUploadManager createVideoFolderOutputPath:outputPath] stringByAppendingPathComponent:filaName]; | |
| 2007 | 2055 | |
| 2008 | 2056 | NSLog(@"video outputPath = %@",editOutputPath); |
| 2009 | 2057 | ... | ... |