mirror of https://github.com/oxen-io/session-ios
Build contact/group exports with data streams
Ideally we'd be using file streams so we wouldn't have to load it all into ram but none of the attachment uploading infrastructure takes streams (yet!) // FREEBIEpull/1/head
parent
fb9f0f9a4d
commit
d48fd158b7
@ -0,0 +1,16 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class PBCodedOutputStream;
|
||||
|
||||
@interface OWSChunkedOutputStream : NSObject
|
||||
|
||||
@property (nonatomic, readonly) PBCodedOutputStream *delegateStream;
|
||||
|
||||
+ (instancetype)streamWithOutputStream:(NSOutputStream *)output;
|
||||
- (void)flush;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,35 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSChunkedOutputStream.h"
|
||||
#import <ProtocolBuffers/CodedOutputStream.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@implementation OWSChunkedOutputStream
|
||||
|
||||
+ (instancetype)streamWithOutputStream:(NSOutputStream *)output
|
||||
{
|
||||
return [[self alloc] initWithOutputStream:output];
|
||||
}
|
||||
|
||||
- (instancetype)initWithOutputStream:(NSOutputStream *)outputStream
|
||||
{
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
_delegateStream = [PBCodedOutputStream streamWithOutputStream:outputStream];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)flush
|
||||
{
|
||||
[self.delegateStream flush];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,15 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSChunkedOutputStream.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class Contact;
|
||||
|
||||
@interface OWSContactsOutputStream : OWSChunkedOutputStream
|
||||
|
||||
- (void)writeContact:(Contact *)contact;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,42 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSContactsOutputStream.h"
|
||||
#import "Contact.h"
|
||||
#import "OWSSignalServiceProtos.pb.h"
|
||||
#import <ProtocolBuffers/CodedOutputStream.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@implementation OWSContactsOutputStream
|
||||
|
||||
- (void)writeContact:(Contact *)contact
|
||||
{
|
||||
OWSSignalServiceProtosContactDetailsBuilder *contactBuilder = [OWSSignalServiceProtosContactDetailsBuilder new];
|
||||
[contactBuilder setName:contact.fullName];
|
||||
[contactBuilder setNumber:contact.textSecureIdentifiers.firstObject];
|
||||
|
||||
NSData *avatarPng;
|
||||
if (contact.image) {
|
||||
OWSSignalServiceProtosContactDetailsAvatarBuilder *avatarBuilder =
|
||||
[OWSSignalServiceProtosContactDetailsAvatarBuilder new];
|
||||
|
||||
[avatarBuilder setContentType:@"image/png"];
|
||||
avatarPng = UIImagePNGRepresentation(contact.image);
|
||||
[avatarBuilder setLength:(uint32_t)avatarPng.length];
|
||||
[contactBuilder setAvatarBuilder:avatarBuilder];
|
||||
}
|
||||
|
||||
NSData *contactData = [[contactBuilder build] data];
|
||||
|
||||
uint32_t contactDataLength = (uint32_t)contactData.length;
|
||||
[self.delegateStream writeRawVarint32:contactDataLength];
|
||||
[self.delegateStream writeRawData:contactData];
|
||||
|
||||
if (contact.image) {
|
||||
[self.delegateStream writeRawData:avatarPng];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,15 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSChunkedOutputStream.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class TSGroupModel;
|
||||
|
||||
@interface OWSGroupsOutputStream : OWSChunkedOutputStream
|
||||
|
||||
- (void)writeGroup:(TSGroupModel *)group;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,42 @@
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSGroupsOutputStream.h"
|
||||
#import "OWSSignalServiceProtos.pb.h"
|
||||
#import "TSGroupModel.h"
|
||||
#import <ProtocolBuffers/CodedOutputStream.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@implementation OWSGroupsOutputStream
|
||||
|
||||
- (void)writeGroup:(TSGroupModel *)group
|
||||
{
|
||||
OWSSignalServiceProtosGroupDetailsBuilder *groupBuilder = [OWSSignalServiceProtosGroupDetailsBuilder new];
|
||||
[groupBuilder setId:group.groupId];
|
||||
[groupBuilder setName:group.groupName];
|
||||
[groupBuilder setMembersArray:group.groupMemberIds];
|
||||
|
||||
NSData *avatarPng;
|
||||
if (group.groupImage) {
|
||||
OWSSignalServiceProtosGroupDetailsAvatarBuilder *avatarBuilder =
|
||||
[OWSSignalServiceProtosGroupDetailsAvatarBuilder new];
|
||||
|
||||
[avatarBuilder setContentType:@"image/png"];
|
||||
avatarPng = UIImagePNGRepresentation(group.groupImage);
|
||||
[avatarBuilder setLength:(uint32_t)avatarPng.length];
|
||||
[groupBuilder setAvatarBuilder:avatarBuilder];
|
||||
}
|
||||
|
||||
NSData *groupData = [[groupBuilder build] data];
|
||||
uint32_t groupDataLength = (uint32_t)groupData.length;
|
||||
[self.delegateStream writeRawVarint32:groupDataLength];
|
||||
[self.delegateStream writeRawData:groupData];
|
||||
|
||||
if (avatarPng) {
|
||||
[self.delegateStream writeRawData:avatarPng];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue