app有些數據是放在阿里雲服務器上的,需要down到本地之後解壓使用,大部分時間這些數據不會改變,所以需要有判斷方法來判斷他是否需要更新,是更新下載,還是直接讀取本地
.h
typedef void (^downloadProgress)(int64_t totalLength, int64_t currentLength); ///<oss下載進度的block
typedef void (^downloadSuccess)(NSString *directoryPath); ///<下載/更新成功之後回調,參數是下載文件的目錄地址
typedef void (^downloadError)(NSError *error); ///<下載/更新失敗之後回調,參數是錯誤信息
typedef void (^NoNeedToUpdate)(NSString *directoryPath); ///<不需要更新的回調
/**
oss下載
@param param 字典參數,name文件名,iszip是否需要解壓縮,type文件類型
@param url 請求文件的cid,需要前接OSSRequestPath,後拼接文件名使用
@param progress 下載進度回調block
@param success 下載成功的回調block
@param fail 下載失敗的回調block
@param notUpdate 不需要更新得回調block
*/
-(void)requestOSSWithParam:(id)param URL:(NSString *)url progress:(downloadProgress)progress succcess:(downloadSuccess)success fail:(downloadError)fail notUpdate:(NoNeedToUpdate)notUpdate;
.m
/**
oss下載
@param param 字典參數,name文件名,iszip是否需要解壓縮,type文件類型
@param url 請求文件的cid,需要前接OSSRequestPath,後拼接文件名使用
@param progress 下載進度回調block
@param success 下載成功的回調block
@param fail 下載失敗的回調block
@param notUpdate 不需要更新得回調block
*/
-(void)requestOSSWithParam:(id)param URL:(NSString *)url progress:(downloadProgress)progress succcess:(downloadSuccess)success fail:(downloadError)fail notUpdate:(NoNeedToUpdate)notUpdate{
//文件名稱
NSString *name = param[@"name"];
//文件拼接名稱類型
NSString *path = [NSString stringWithFormat:@"%@%@",name, param[@"type"]];
//文件是否是zip類型
BOOL isZip = [param[@"iszip"]integerValue];
//獲取文件的地址(目錄)(沙盒/oss/cid/mod-uid)
NSString *filePath = [AEFileManager getFilePathWithNames:@[[AEFileManager getCidDownloadPathWithCid:url],name]];
//獲取文件的更新時間文件地址(沙盒/oss/cid/mod_time)
NSString *fileUpdateTime = [AEFileManager getFilePathWithNames:@[[AEFileManager getCidDownloadPathWithCid:url],[NSString stringWithFormat:@"%@_time",name]]];
//更新文件時間的字符串(時間)
__block NSString *downloadUpdateTime = nil;
//檢查
OSSHeadObjectRequest * head = [OSSHeadObjectRequest new];
head.bucketName = OSSBucketName;
head.objectKey = [AEFileManager getFilePathWithNames:@[OSSRequestPath, url, path]];
OSSTask * headTask = [_client headObject:head];
[headTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
OSSHeadObjectResult * headResult = task.result;
NSDictionary *dict = headResult.objectMeta;
//獲取最後一次更新時間
NSString *serverDate = [dict objectForKey:@"Last-Modified"];
ZLog(@"serverDate:%@",serverDate);
NSError *error;
//本地的上次更新得時間
NSString *localDate = [[NSString alloc]initWithContentsOfFile:fileUpdateTime encoding:NSUTF8StringEncoding error:&error];
//有本地時間並且與返回時間一致
if ([localDate isEqualToString:serverDate]&&localDate) {
//不需要更新,回調提示
if (notUpdate) {
notUpdate(filePath);
}
}else{
//沒有時間(沒有下載過文件)或者時間不一致(需要更新)
//設置請求頭
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
request.bucketName = OSSBucketName;
request.objectKey = [AEFileManager getFilePathWithNames:@[OSSRequestPath, url, path]];
//下載進度返回
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
//回到主線程回調
dispatch_async(dispatch_get_main_queue(), ^{
if (progress) {
progress(totalBytesExpectedToWrite, totalBytesWritten);
}
});
};
//設置新的文件更新時間
downloadUpdateTime = [NSString stringWithString:serverDate];
//開始下載
OSSTask * getTask = [_client getObject:request];
[getTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
OSSGetObjectResult * getResult = task.result;
//下載成功
if (!task.error) {
NSLog(@"download object success!");
//壓縮包的文件地址(沙盒/oss/ky1/1mod.zip)
NSString *zipfilePath = [AEFileManager getFilePathWithNames:@[[AEFileManager getCidDownloadPathWithCid:url],[NSString stringWithFormat:@"1%@",path]]];
ZLog(@"zipfilePath:%@",zipfilePath);
//下載類型判斷是否需要解壓
if (isZip) {
//保存文件
id data = getResult.downloadedData;
BOOL isSuccess;
if ([data isKindOfClass:[NSData class]]) {
isSuccess = [AEFileManager saveDataForPath:zipfilePath withData:data];
}else {
isSuccess = [data writeToFile:zipfilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
// ZLog(@"isSuccess:%d,zipfile:%@,data:%@",isSuccess,zipfilePath, data);
//保存成功
if (isSuccess) {
dispatch_async(dispatch_get_main_queue(), ^{
//解壓保存,解壓地址是沙盒/cid
NSError *error;
if ([SSZipArchive unzipFileAtPath:zipfilePath toDestination:[AEFileManager getFilePathWithNames:@[self.cachePath, url]] overwrite:YES password:nil error:&error delegate:self]) {
//刪掉源文件
[[NSFileManager defaultManager]removeItemAtPath:zipfilePath error:nil];
//保存時間文件
[AEFileManager saveStringForPath:fileUpdateTime withData:downloadUpdateTime];
//傳遞出去
success(filePath);
}else{
NSLog(@"解壓失敗 : %@",error);
fail(error);
}
});
}else{
//保存失敗
NSLog(@"upzip object failed");
fail(task.error);
}
}else{ ///<不需要解壓
//保存時間文件
[AEFileManager saveStringForPath:fileUpdateTime withData:downloadUpdateTime];
//直接傳遞
success(filePath);
}
} else { ///<下載失敗
NSLog(@"download object failed, error: %@" ,task.error);
fail(task.error);
}
return task;
}];
}
} else {
//獲取時間失敗
NSLog(@"head object error: %@", task.error);
if (fail) {
fail(task.error);
}
}
return task;
}];
}