Sfoglia il codice sorgente

解决文件本地路径不对问题

zwp 5 mesi fa
parent
commit
d0d2371690

+ 11 - 1
AIIM/Common/Network/aliOSS/OSSManager.m

@@ -247,6 +247,16 @@
 
 -(void)PrasyncResumableUploadFile:(NSString *)objectKey localFilePath:(NSString *)filePath thrid:(NSString *)strtime{
     
+    NSLog(@"PrasyncResumableUploadFile objectKey: %@ filePath: %@", objectKey, filePath);
+    
+    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
+    NSString *fileName = [NSURL fileURLWithPath:filePath].lastPathComponent;
+    NSString *path = [documentsDirectoryURL URLByAppendingPathComponent:fileName].path;
+    if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
+        NSLog(@"PrasyncResumableUploadFile error: %@", path);
+        return;
+    }
+    
     // 获取UploadId上传文件。
     OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
     resumableUpload.bucketName = self.OSSInfo[@"bucket"];
@@ -278,7 +288,7 @@
     // 将参数deleteUploadIdOnCancelling设置为NO,表示不删除断点记录文件,上传失败后将从断点记录处继续上传直到文件上传完成。如果不设置此参数,即保留默认值YES,表示删除断点记录文件,下次再上传同一文件时则重新上传。
     resumableUpload.deleteUploadIdOnCancelling = NO;
 
-    resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:filePath];
+    resumableUpload.uploadingFileURL = [NSURL URLWithString:path];
     OSSTask * resumeTask = [[OSSManager sharedManager].defaultClient resumableUpload:resumableUpload];
     [self addUploadTanckobj:resumeTask thrid:strtime];
     [resumeTask continueWithBlock:^id(OSSTask *task) {

+ 21 - 44
AIIM/Controller/chat/chetCell/ChatMessageModel.m

@@ -113,10 +113,10 @@
     // 其他自定义字段...
     self.readStatus = [self getReadStatus];
     
-    BOOL contain = [[NSFileManager defaultManager] fileExistsAtPath:self.localurl];
     NSString *localPath = [self checkHasLocalPath];
-    if (!contain && localPath.length) {
+    if (localPath.length) {
         self.localurl = localPath;
+        [self updateFileSizeString];
     }
 }
 
@@ -347,7 +347,6 @@
 //下载视频文件
 -(void)downloadFileIfNeed:(void(^)(NSInteger persent))loading{
     if ([[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
-        self.customFileSize = [self getFileSize];
         if (loading) {
             loading(100);
         }
@@ -355,28 +354,6 @@
         return;
     }
     
-    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-    NSURL *tempUrl = [NSURL URLWithString:self.url];
-    NSString *fileName = tempUrl.lastPathComponent;
-    NSString *fileExtension = [fileName pathExtension];
-    NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
-    documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
-    if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
-        NSLog(@"downloadFileIfNeed 1: %@", documentsDirectoryURL.path);
-        NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
-        NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
-        [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
-        [mutableDict setObject:extend forKey:@"extend"];
-        self.localurl = documentsDirectoryURL.path;
-        NSDictionary *NewMsg = [mutableDict copy];
-        [ChatsStore.shareInstance reciveMsg:NewMsg];
-        self.customFileSize = [self getFileSize];
-        if (loading) {
-            loading(100);
-        }
-        return;
-    }
-    
     if (![self.url hasPrefix:@"http"]) {
         NSLog(@"文件URL异常:%@", self.formerMessage);
         return;
@@ -391,7 +368,7 @@
             self.localurl = res[@"filePath"];
             NSDictionary *NewMsg = [mutableDict copy];
             [ChatsStore.shareInstance reciveMsg:NewMsg];
-            self.customFileSize = [self getFileSize];
+            [self updateFileSizeString];
             if (loading) {
                 loading(100);
             }
@@ -427,7 +404,7 @@
 
 #pragma mark - 消息状态管理
 
-- (NSString *)getFileSize {
+- (void)updateFileSizeString {
     NSString *filePath = self.localurl;
 
     NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -450,31 +427,31 @@
         NSLog(@"文件大小: %.2f KB (%.2f MB)", fileSizeKB, fileSizeMB);
         string = [NSString stringWithFormat:@"%.2fMB", fileSizeMB];
     }
-    return string;
+    _customFileSize = string;
 }
 
-- (NSString *)customFileSize {
-    if (!_customFileSize) {
-        _customFileSize = [self getFileSize];
-    }
-    return _customFileSize;
-}
 
 - (NSString *)checkHasLocalPath {
-    if ([[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
-        self.customFileSize = [self getFileSize];
-        NSLog(@"checkHasLocalPath 0: %@", self.localurl);
-        return self.localurl;
+    if (!self.isUploadFile) {
+        return nil;
     }
     
     NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-    NSURL *tempUrl = [NSURL URLWithString:self.url];
-    NSString *fileName = tempUrl.lastPathComponent;
-    NSString *fileExtension = [fileName pathExtension];
-    NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
-    NSString *path = [documentsDirectoryURL URLByAppendingPathComponent:newfileName].path;
+    
+    NSString *fileName;
+    NSURL *localURL = [NSURL URLWithString:self.localurl];
+    if (localURL) {
+        fileName = localURL.lastPathComponent;
+    } else {
+        fileName = [NSURL URLWithString:self.url].lastPathComponent;
+    }
+    if (fileName.length == 0) {
+        return nil;
+    }
+    
+    NSString *path = [documentsDirectoryURL URLByAppendingPathComponent:fileName].path;
     if ([[NSFileManager defaultManager] fileExistsAtPath:path]){
-        NSLog(@"checkHasLocalPath 0: %@", path);
+        NSLog(@"checkHasLocalPath: %@", path);
         return path;
     }
     return nil;