1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 09:27:55 +01:00
Telegram/Telegraph/TGPreparedLocalImageMessage.m

150 lines
5.9 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
/*
* This is the source code of Telegram for iOS v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Peter Iakovlev, 2013.
*/
#import "TGPreparedLocalImageMessage.h"
#import "TGMessage.h"
2015-10-01 18:19:52 +02:00
#import "TGAppDelegate.h"
2014-07-10 16:11:09 +02:00
@implementation TGPreparedLocalImageMessage
2015-10-01 18:19:52 +02:00
+ (instancetype)messageWithImageData:(NSData *)imageData imageSize:(CGSize)imageSize thumbnailData:(NSData *)thumbnailData thumbnailSize:(CGSize)thumbnailSize assetUrl:(NSString *)assetUrl caption:(NSString *)caption replyMessage:(TGMessage *)replyMessage
2014-07-10 16:11:09 +02:00
{
#ifdef DEBUG
NSAssert(imageData != nil, @"imageData should not be nil");
NSAssert(thumbnailData != nil, @"thumbnailData should not be nil");
#endif
TGPreparedLocalImageMessage *message = [[TGPreparedLocalImageMessage alloc] init];
message.imageSize = imageSize;
message.thumbnailSize = thumbnailSize;
message.assetUrl = assetUrl;
message.localImageDataPath = [self _fileUrlForStoredData:imageData];
message.localThumbnailDataPath = [self _fileUrlForStoredData:thumbnailData];
2015-10-01 18:19:52 +02:00
message.caption = caption;
message.replyMessage = replyMessage;
2014-07-10 16:11:09 +02:00
return message;
}
2015-10-01 18:19:52 +02:00
+ (instancetype)messageWithLocalImageDataPath:(NSString *)localImageDataPath imageSize:(CGSize)imageSize localThumbnailDataPath:(NSString *)localThumbnailDataPath thumbnailSize:(CGSize)thumbnailSize assetUrl:(NSString *)assetUrl caption:(NSString *)caption replyMessage:(TGMessage *)replyMessage
2014-07-10 16:11:09 +02:00
{
#ifdef DEBUG
NSAssert(localImageDataPath != nil, @"localImageDataPath should not be nil");
NSAssert(localThumbnailDataPath != nil, @"localThumbnailDataPath should not be nil");
#endif
TGPreparedLocalImageMessage *message = [[TGPreparedLocalImageMessage alloc] init];
message.imageSize = imageSize;
message.thumbnailSize = thumbnailSize;
message.assetUrl = assetUrl;
message.localImageDataPath = localImageDataPath;
message.localThumbnailDataPath = localThumbnailDataPath;
2015-10-01 18:19:52 +02:00
message.caption = caption;
message.replyMessage = replyMessage;
return message;
}
+ (instancetype)messageByCopyingMessageData:(TGPreparedLocalImageMessage *)source
{
TGPreparedLocalImageMessage *message = [[TGPreparedLocalImageMessage alloc] init];
message.imageSize = source.imageSize;
message.thumbnailSize = source.thumbnailSize;
message.assetUrl = source.assetUrl;
message.localImageDataPath = [TGPreparedLocalImageMessage _fileUrlForStoredFile:source.localImageDataPath];
message.localThumbnailDataPath = [TGPreparedLocalImageMessage _fileUrlForStoredFile:source.localThumbnailDataPath];
message.caption = source.caption;
message.replyMessage = source.replyMessage;
2014-07-10 16:11:09 +02:00
return message;
}
+ (NSString *)_fileUrlForStoredData:(NSData *)data
{
2015-10-01 18:19:52 +02:00
NSString *documentsDirectory = [TGAppDelegate documentsPath];
2014-07-10 16:11:09 +02:00
NSString *uploadDirectory = [documentsDirectory stringByAppendingPathComponent:@"upload"];
if (![[NSFileManager defaultManager] fileExistsAtPath:uploadDirectory])
[[NSFileManager defaultManager] createDirectoryAtPath:uploadDirectory withIntermediateDirectories:true attributes:nil error:nil];
int64_t randomId = 0;
arc4random_buf(&randomId, sizeof(randomId));
NSString *imagePathComponent = [[NSString alloc] initWithFormat:@"%" PRIx64 ".bin", randomId];
NSString *filePath = [uploadDirectory stringByAppendingPathComponent:imagePathComponent];
[data writeToFile:filePath atomically:false];
return [@"file://" stringByAppendingString:filePath];
}
2015-10-01 18:19:52 +02:00
+ (NSString *)_fileUrlForStoredFile:(NSString *)storedFilePath
{
NSString *documentsDirectory = [TGAppDelegate documentsPath];
NSString *uploadDirectory = [documentsDirectory stringByAppendingPathComponent:@"upload"];
if (![[NSFileManager defaultManager] fileExistsAtPath:uploadDirectory])
[[NSFileManager defaultManager] createDirectoryAtPath:uploadDirectory withIntermediateDirectories:true attributes:nil error:nil];
int64_t randomId = 0;
arc4random_buf(&randomId, sizeof(randomId));
NSString *imagePathComponent = [[NSString alloc] initWithFormat:@"%" PRIx64 ".bin", randomId];
NSString *filePath = [uploadDirectory stringByAppendingPathComponent:imagePathComponent];
[[NSFileManager defaultManager] copyItemAtURL:[NSURL URLWithString:storedFilePath] toURL:[NSURL URLWithString:[@"file://" stringByAppendingString:filePath]] error:nil];
return [@"file://" stringByAppendingString:filePath];
}
2014-07-10 16:11:09 +02:00
- (TGMessage *)message
{
TGMessage *message = [[TGMessage alloc] init];
message.mid = self.mid;
message.date = self.date;
2015-10-01 18:19:52 +02:00
message.isBroadcast = self.isBroadcast;
message.messageLifetime = self.messageLifetime;
NSMutableArray *attachments = [[NSMutableArray alloc] init];
2014-07-10 16:11:09 +02:00
TGImageMediaAttachment *imageAttachment = [[TGImageMediaAttachment alloc] init];
TGImageInfo *imageInfo = [[TGImageInfo alloc] init];
[imageInfo addImageWithSize:_imageSize url:[self localImageDataPath]];
[imageInfo addImageWithSize:_thumbnailSize url:[self localThumbnailDataPath]];
imageAttachment.imageInfo = imageInfo;
2015-10-01 18:19:52 +02:00
imageAttachment.caption = self.caption;
[attachments addObject:imageAttachment];
2014-07-10 16:11:09 +02:00
TGLocalMessageMetaMediaAttachment *mediaMeta = [[TGLocalMessageMetaMediaAttachment alloc] init];
mediaMeta.imageUrlToDataFile[[self localImageDataPath]] = [self localImageDataPath];
mediaMeta.imageUrlToDataFile[[self localThumbnailDataPath]] = [self localThumbnailDataPath];
2015-10-01 18:19:52 +02:00
[attachments addObject:mediaMeta];
2016-02-25 01:03:51 +01:00
if (self.replyMessage != nil)
2015-10-01 18:19:52 +02:00
{
TGReplyMessageMediaAttachment *replyMedia = [[TGReplyMessageMediaAttachment alloc] init];
2016-02-25 01:03:51 +01:00
replyMedia.replyMessageId = self.replyMessage.mid;
replyMedia.replyMessage = self.replyMessage;
2015-10-01 18:19:52 +02:00
[attachments addObject:replyMedia];
}
2014-07-10 16:11:09 +02:00
2015-10-01 18:19:52 +02:00
message.mediaAttachments = attachments;
2014-07-10 16:11:09 +02:00
return message;
}
@end