VideoMessage.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "VideoMessage.h"
  21. #import "ImageData.h"
  22. #import "VideoData.h"
  23. #import "NSString+Hex.h"
  24. #import "BundleUtil.h"
  25. #import "UTIConverter.h"
  26. #import "Utils.h"
  27. #import "ThreemaFramework/ThreemaFramework-swift.h"
  28. @implementation VideoMessage
  29. @dynamic progress;
  30. @dynamic videoSize;
  31. @dynamic videoBlobId;
  32. @dynamic encryptionKey;
  33. @dynamic video;
  34. @dynamic thumbnail;
  35. @dynamic duration;
  36. @synthesize thumbnailWithPlayOverlay = _thumbnailWithPlayOverlay;
  37. - (NSString*)logText {
  38. int seconds = self.duration.intValue;
  39. int minutes = seconds / 60;
  40. seconds -= minutes * 60;
  41. return [NSString stringWithFormat:@"%@ (%02d:%02d, %@)", NSLocalizedString(@"video", nil), minutes, seconds, [self blobGetFilename]];
  42. }
  43. - (NSString*)previewText {
  44. return NSLocalizedString(@"video", nil);
  45. }
  46. - (NSData *)blobGetData {
  47. if (self.video) {
  48. return self.video.data;
  49. }
  50. return nil;
  51. }
  52. - (NSData *)blobGetId {
  53. return self.videoBlobId;
  54. }
  55. - (NSData *)blobGetEncryptionKey {
  56. return self.encryptionKey;
  57. }
  58. - (NSNumber *)blobGetSize {
  59. return self.videoSize;
  60. }
  61. - (void)blobSetData:(NSData *)data {
  62. VideoData *dbData = [NSEntityDescription
  63. insertNewObjectForEntityForName:@"VideoData"
  64. inManagedObjectContext:self.managedObjectContext];
  65. dbData.data = data;
  66. self.video = dbData;
  67. }
  68. - (NSData *)blobGetThumbnail {
  69. if (self.thumbnail) {
  70. return self.thumbnail.data;
  71. }
  72. return nil;
  73. }
  74. - (NSString *)blobGetUTI {
  75. return UTTYPE_VIDEO;
  76. }
  77. - (UIImage *)thumbnailWithPlayOverlay {
  78. if (_thumbnailWithPlayOverlay == nil) {
  79. _thumbnailWithPlayOverlay = [Utils makeThumbWithOverlayFor:self.thumbnail.uiImage];
  80. }
  81. return _thumbnailWithPlayOverlay;
  82. }
  83. - (NSString *)blobGetFilename {
  84. return [NSString stringWithFormat: @"%@.%@", [NSString stringWithHexData:self.id], MEDIA_EXTENSION_VIDEO];
  85. }
  86. - (NSString *)blobGetWebFilename {
  87. return [NSString stringWithFormat: @"threema-%@-video.%@", [DateFormatter getDateForWeb:self.date], MEDIA_EXTENSION_VIDEO];
  88. }
  89. - (void)blobUpdateProgress:(NSNumber *)progress {
  90. self.progress = progress;
  91. }
  92. - (NSNumber *)blobGetProgress {
  93. return self.progress;
  94. }
  95. #ifdef DEBUG
  96. #else
  97. - (NSString *)debugDescription
  98. {
  99. return [NSString stringWithFormat:@"<%@: %p> %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@", [self class], self, @"progress = ", self.progress.description, @"videoBlobId = ", @"***", @"encryptionKey = ", @"***", @"videoSize = ", self.videoSize.description, @"video = ", self.video.description, @"thumbnail = ", self.thumbnail.description, @"duration = ", self.duration.description];
  100. }
  101. #endif
  102. #pragma mark ExternalStorageInfo
  103. - (NSString *)getFilename {
  104. return self.video != nil ? [self getFilename:self.video.data] : nil;
  105. }
  106. - (NSString *)getThumbnailname {
  107. return self.thumbnail != nil ? [self getFilename:self.thumbnail.data] : nil;
  108. }
  109. - (NSString *)getFilename:(NSData *)ofData {
  110. if (ofData != nil && [ofData respondsToSelector:NSSelectorFromString(@"filename")]) {
  111. return [ofData performSelector:NSSelectorFromString(@"filename")];
  112. }
  113. return nil;
  114. }
  115. @end