Add "is uploaded" property to attachment streams.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 27fb0dd34c
commit 865d9d7b96

@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 12/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSYapDatabaseObject.h"
@ -19,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
encryptionKey:(NSData *)encryptionKey
contentType:(NSString *)contentType;
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion;
@end
NS_ASSUME_NONNULL_END

@ -1,12 +1,13 @@
// Created by Frederic Jacobs on 12/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSAttachment.h"
#import "MIMETypeUtil.h"
NS_ASSUME_NONNULL_BEGIN
NSUInteger const TSAttachmentSchemaVersion = 2;
NSUInteger const TSAttachmentSchemaVersion = 3;
@interface TSAttachment ()
@ -40,7 +41,17 @@ NSUInteger const TSAttachmentSchemaVersion = 2;
return self;
}
if (_attachmentSchemaVersion < 2) {
if (_attachmentSchemaVersion < TSAttachmentSchemaVersion) {
[self upgradeFromAttachmentSchemaVersion:_attachmentSchemaVersion];
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
}
return self;
}
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
{
if (attachmentSchemaVersion < 2) {
if (!_serverId) {
_serverId = [self.uniqueId integerValue];
if (!_serverId) {
@ -48,10 +59,6 @@ NSUInteger const TSAttachmentSchemaVersion = 2;
}
}
}
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
return self;
}
+ (NSString *)collection {

@ -23,6 +23,9 @@ NS_ASSUME_NONNULL_BEGIN
// messages received from other clients
@property (nullable, nonatomic) NSData *digest;
// This only applies for attachments being uploaded.
@property (atomic) BOOL isUploaded;
#if TARGET_OS_IPHONE
- (nullable UIImage *)image;
#endif

@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 17/12/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSAttachmentStream.h"
#import "MIMETypeUtil.h"
@ -20,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
_contentType = contentType;
_isDownloaded = YES;
// TSAttachmentStream doesn't have any "incoming vs. outgoing"
// state, but this constructor is used only for new outgoing
// attachments which haven't been uploaded yet.
_isUploaded = NO;
return self;
}
@ -34,10 +39,27 @@ NS_ASSUME_NONNULL_BEGIN
_contentType = pointer.contentType;
_isDownloaded = YES;
// TSAttachmentStream doesn't have any "incoming vs. outgoing"
// state, but this constructor is used only for new incoming
// attachments which don't need to be uploaded.
_isUploaded = YES;
return self;
}
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
{
[super upgradeFromAttachmentSchemaVersion:attachmentSchemaVersion];
if (attachmentSchemaVersion < 3) {
// We want to treat any legacy TSAttachmentStream as though
// they have already been uploaded. If it needs to be reuploaded,
// the OWSUploadingService will update this progress when the
// upload begins.
self.isUploaded = YES;
}
}
#pragma mark - TSYapDatabaseModel overrides
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction

@ -12,9 +12,17 @@ NS_ASSUME_NONNULL_BEGIN
@interface TSOutgoingMessage : TSMessage
typedef NS_ENUM(NSInteger, TSOutgoingMessageState) {
// The message is either:
// a) Enqueued for sending.
// b) Waiting on attachment upload(s).
// c) Being sent to the service.
TSOutgoingMessageStateAttemptingOut,
// The failure state.
TSOutgoingMessageStateUnsent,
// The message has been sent to the service, but not received by any recipient client.
TSOutgoingMessageStateSent,
// The message has been sent to the service, but not received by at least one recipient client.
// A recipient may have more than one client, and group message may have more than one recipient.
TSOutgoingMessageStateDelivered
};

@ -1,5 +1,6 @@
// Created by Michael Kirk on 10/18/16.
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@ -7,6 +8,10 @@ NS_ASSUME_NONNULL_BEGIN
@class TSNetworkManager;
@class TSAttachmentStream;
extern NSString *const kAttachmentUploadProgressNotification;
extern NSString *const kAttachmentUploadProgressKey;
extern NSString *const kAttachmentUploadAttachmentIDKey;
@interface OWSUploadingService : NSObject
- (instancetype)init NS_UNAVAILABLE;

@ -12,6 +12,10 @@
NS_ASSUME_NONNULL_BEGIN
NSString *const kAttachmentUploadProgressNotification = @"kAttachmentUploadProgressNotification";
NSString *const kAttachmentUploadProgressKey = @"kAttachmentUploadProgressKey";
NSString *const kAttachmentUploadAttachmentIDKey = @"kAttachmentUploadAttachmentIDKey";
@interface OWSUploadingService ()
@property (nonatomic, readonly) TSNetworkManager *networkManager;
@ -79,9 +83,11 @@ NS_ASSUME_NONNULL_BEGIN
location:location
attachmentId:attachmentStream.uniqueId
success:^{
DDLogInfo(@"%@ Uploaded attachment.", self.tag);
OWSAssert([NSThread isMainThread]);
DDLogInfo(@"%@ Uploaded attachment: %p.", self.tag, attachmentStream);
attachmentStream.serverId = serverId;
attachmentStream.isDownloaded = YES;
attachmentStream.isUploaded = YES;
[attachmentStream save];
successHandler();
@ -111,20 +117,18 @@ NS_ASSUME_NONNULL_BEGIN
AFURLSessionManager *manager = [[AFURLSessionManager alloc]
initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[self fireProgressNotification:0 attachmentId:attachmentId];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithRequest:request
fromData:cipherText
progress:^(NSProgress *_Nonnull uploadProgress) {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:@"attachmentUploadProgress"
object:nil
userInfo:@{
@"progress" : @(uploadProgress.fractionCompleted),
@"attachmentId" : attachmentId
}];
[self fireProgressNotification:uploadProgress.fractionCompleted attachmentId:attachmentId];
}
completionHandler:^(NSURLResponse *_Nonnull response, id _Nullable responseObject, NSError *_Nullable error) {
OWSAssert([NSThread isMainThread]);
if (error) {
[self fireProgressNotification:0 attachmentId:attachmentId];
return failureHandler(error);
}
@ -137,11 +141,26 @@ NS_ASSUME_NONNULL_BEGIN
}
successHandler();
[self fireProgressNotification:1 attachmentId:attachmentId];
}];
[uploadTask resume];
}
- (void)fireProgressNotification:(CGFloat)progress attachmentId:(NSString *)attachmentId
{
dispatch_async(dispatch_get_main_queue(), ^{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:kAttachmentUploadProgressNotification
object:nil
userInfo:@{
kAttachmentUploadProgressKey : @(progress),
kAttachmentUploadAttachmentIDKey : attachmentId
}];
});
}
#pragma mark - Logging
+ (NSString *)tag

Loading…
Cancel
Save