2017-03-10 14:46:36 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import MobileCoreServices
|
|
|
|
|
|
|
|
enum SignalAttachmentError: String {
|
|
|
|
case fileSizeTooLarge
|
|
|
|
case invalidData
|
|
|
|
case couldNotParseImage
|
|
|
|
case couldNotConvertToJpeg
|
|
|
|
case invalidFileFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a possible attachment to upload.
|
|
|
|
// The attachment may be invalid.
|
|
|
|
//
|
|
|
|
// Signal attachments are subject to validation and
|
|
|
|
// in some cases, file format conversion.
|
|
|
|
//
|
|
|
|
// This class gathers that logic. It offers factory methods
|
|
|
|
// for attachments that do the necessary work.
|
|
|
|
//
|
|
|
|
// The return value for the factory methods will be nil if the input is nil.
|
|
|
|
//
|
|
|
|
// [SignalAttachment hasError] will be true for non-valid attachments.
|
|
|
|
//
|
|
|
|
// TODO: Perhaps do conversion off the main thread?
|
|
|
|
// TODO: Show error on error.
|
|
|
|
// TODO: Show progress on upload.
|
|
|
|
class SignalAttachment: NSObject {
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
static let TAG = "[SignalAttachment]"
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Properties
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
let data: Data!
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Attachment types are identified using UTIs.
|
|
|
|
//
|
|
|
|
// See: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
|
2017-03-10 21:09:37 +01:00
|
|
|
let dataUTI: String!
|
|
|
|
|
|
|
|
var error: SignalAttachmentError? {
|
2017-03-10 14:46:36 +01:00
|
|
|
didSet {
|
|
|
|
AssertIsOnMainThread()
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(oldValue == nil)
|
|
|
|
Logger.verbose("\(SignalAttachment.TAG) Attachment has error: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
// To avoid redundant work of repeatedly compressing/uncompressing
|
|
|
|
// images, we cache the UIImage associated with this attachment if
|
|
|
|
// possible.
|
|
|
|
public var image: UIImage?
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Constants
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
/**
|
|
|
|
* Media Size constraints from Signal-Android
|
|
|
|
* (org/thoughtcrime/securesms/mms/PushMediaConstraints.java)
|
|
|
|
*/
|
2017-03-10 21:09:37 +01:00
|
|
|
static let kMaxFileSizeGif = 5 * 1024 * 1024
|
|
|
|
static let kMaxFileSizeImage = 420 * 1024
|
|
|
|
static let kMaxFileSizeVideo = 100 * 1024 * 1024
|
|
|
|
static let kMaxFileSizeAudio = 100 * 1024 * 1024
|
2017-03-10 14:46:36 +01:00
|
|
|
// TODO: What should the max file size on "other" attachments be?
|
2017-03-10 21:09:37 +01:00
|
|
|
static let kMaxFileSizeGeneric = 100 * 1024 * 1024
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Constructor
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// This method should not be called directly; use the factory
|
|
|
|
// methods instead.
|
2017-03-10 21:09:37 +01:00
|
|
|
internal required init(data: Data!, dataUTI: String!) {
|
2017-03-10 14:46:36 +01:00
|
|
|
self.data = data
|
|
|
|
self.dataUTI = dataUTI
|
|
|
|
super.init()
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
public func hasError() -> Bool {
|
|
|
|
return error != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the MIME type for this attachment or nil if no MIME type
|
|
|
|
// can be identified.
|
|
|
|
public func mimeType() -> String? {
|
|
|
|
let mimeType = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassMIMEType)
|
|
|
|
guard mimeType != nil else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return mimeType?.takeRetainedValue() as? String
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
// Returns the file extension for this attachment or nil if no file extension
|
|
|
|
// can be identified.
|
|
|
|
public func fileExtension() -> String? {
|
|
|
|
let fileExtension = UTTypeCopyPreferredTagWithClass(dataUTI as CFString, kUTTagClassFilenameExtension)
|
|
|
|
guard fileExtension != nil else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fileExtension?.takeRetainedValue() as? String
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns the set of UTIs that correspond to valid _input_ image formats
|
|
|
|
// for Signal attachments.
|
|
|
|
//
|
|
|
|
// Image attachments may be converted to another image format before
|
|
|
|
// being uploaded.
|
|
|
|
//
|
|
|
|
// TODO: We need to finalize which formats we support.
|
|
|
|
private class func inputImageUTISet() -> Set<String>! {
|
|
|
|
return [
|
|
|
|
kUTTypeJPEG as String,
|
|
|
|
kUTTypeGIF as String,
|
2017-03-10 21:09:37 +01:00
|
|
|
kUTTypePNG as String
|
2017-03-10 14:46:36 +01:00
|
|
|
]
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns the set of UTIs that correspond to valid _output_ image formats
|
|
|
|
// for Signal attachments.
|
|
|
|
//
|
|
|
|
// TODO: We need to finalize which formats we support.
|
|
|
|
private class func outputImageUTISet() -> Set<String>! {
|
|
|
|
return [
|
|
|
|
kUTTypeJPEG as String,
|
|
|
|
kUTTypeGIF as String,
|
2017-03-10 21:09:37 +01:00
|
|
|
kUTTypePNG as String
|
2017-03-10 14:46:36 +01:00
|
|
|
]
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns the set of UTIs that correspond to valid video formats
|
|
|
|
// for Signal attachments.
|
|
|
|
//
|
|
|
|
// TODO: We need to finalize which formats we support.
|
|
|
|
private class func videoUTISet() -> Set<String>! {
|
|
|
|
return [
|
2017-03-10 21:09:37 +01:00
|
|
|
kUTTypeMPEG4 as String
|
2017-03-10 14:46:36 +01:00
|
|
|
]
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns the set of UTIs that correspond to valid audio formats
|
|
|
|
// for Signal attachments.
|
|
|
|
//
|
|
|
|
// TODO: We need to finalize which formats we support.
|
|
|
|
private class func audioUTISet() -> Set<String>! {
|
|
|
|
return [
|
|
|
|
kUTTypeMP3 as String,
|
2017-03-10 21:09:37 +01:00
|
|
|
kUTTypeMPEG4Audio as String
|
2017-03-10 14:46:36 +01:00
|
|
|
]
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns the set of UTIs that correspond to valid input formats
|
|
|
|
// for Signal attachments.
|
|
|
|
public class func validInputUTISet() -> Set<String>! {
|
|
|
|
return inputImageUTISet().union(videoUTISet().union(audioUTISet()))
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
public func isImage() -> Bool {
|
|
|
|
return SignalAttachment.outputImageUTISet().contains(dataUTI)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isVideo() -> Bool {
|
|
|
|
return SignalAttachment.videoUTISet().contains(dataUTI)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func isAudio() -> Bool {
|
|
|
|
return SignalAttachment.audioUTISet().contains(dataUTI)
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Returns an attachment from the pasteboard, or nil if no attachment
|
|
|
|
// can be found.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may not be valid.
|
|
|
|
// Check the attachment's error property.
|
|
|
|
public class func attachmentFromPasteboard() -> SignalAttachment? {
|
|
|
|
guard UIPasteboard.general.numberOfItems == 1 else {
|
|
|
|
// Ignore pasteboard if it contains multiple items.
|
|
|
|
//
|
|
|
|
// TODO: Should we try to use the first?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let pasteboardUTISet = Set(UIPasteboard.general.types)
|
|
|
|
for dataUTI in inputImageUTISet() {
|
|
|
|
if pasteboardUTISet.contains(dataUTI) {
|
|
|
|
let imageData = UIPasteboard.general.data(forPasteboardType:dataUTI)
|
|
|
|
return imageAttachment(withData : imageData, dataUTI : dataUTI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for dataUTI in videoUTISet() {
|
|
|
|
if pasteboardUTISet.contains(dataUTI) {
|
|
|
|
let imageData = UIPasteboard.general.data(forPasteboardType:dataUTI)
|
|
|
|
return videoAttachment(withData : imageData, dataUTI : dataUTI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for dataUTI in audioUTISet() {
|
|
|
|
if pasteboardUTISet.contains(dataUTI) {
|
|
|
|
let imageData = UIPasteboard.general.data(forPasteboardType:dataUTI)
|
|
|
|
return audioAttachment(withData : imageData, dataUTI : dataUTI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: We could handle generic attachments at this point.
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Image Attachments
|
|
|
|
|
|
|
|
// Factory method for an image attachment.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may not be valid.
|
|
|
|
// Check the attachment's error property.
|
2017-03-10 21:09:37 +01:00
|
|
|
public class func imageAttachment(withData imageData: Data?, dataUTI: String!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(dataUTI.characters.count > 0)
|
|
|
|
|
|
|
|
assert(imageData != nil)
|
|
|
|
guard let imageData = imageData else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
let attachment = SignalAttachment(data : imageData, dataUTI: dataUTI)
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
guard inputImageUTISet().contains(dataUTI) else {
|
|
|
|
attachment.error = .invalidFileFormat
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
guard imageData.count > 0 else {
|
|
|
|
assert(imageData.count > 0)
|
|
|
|
attachment.error = .invalidData
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
if dataUTI == kUTTypeGIF as String {
|
2017-03-10 21:09:37 +01:00
|
|
|
guard imageData.count <= kMaxFileSizeGif else {
|
2017-03-10 14:46:36 +01:00
|
|
|
attachment.error = .fileSizeTooLarge
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
// We don't re-encode GIFs as JPEGs, presumably in case they are
|
|
|
|
// animated.
|
|
|
|
//
|
|
|
|
// TODO: Consider re-encoding non-animated GIFs as JPEG?
|
2017-03-10 21:09:37 +01:00
|
|
|
Logger.verbose("\(TAG) Sending raw \(attachment.mimeType()) to retain any animation")
|
2017-03-10 14:46:36 +01:00
|
|
|
return attachment
|
|
|
|
} else {
|
|
|
|
guard let image = UIImage(data:imageData) else {
|
|
|
|
attachment.error = .couldNotParseImage
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
attachment.image = image
|
|
|
|
|
|
|
|
if isInputImageValidOutputImage(image: image, imageData: imageData, dataUTI: dataUTI) {
|
|
|
|
Logger.verbose("\(TAG) Sending raw \(attachment.mimeType())")
|
|
|
|
return attachment
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
Logger.verbose("\(TAG) Compressing attachment as image/jpeg")
|
|
|
|
return compressImageAsJPEG(image : image, attachment : attachment)
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
// If the proposed attachment already is a JPEG, and already conforms to the
|
|
|
|
// file size and content size limits, don't recompress it.
|
|
|
|
//
|
|
|
|
// TODO: Should non-JPEGs always be converted to JPEG?
|
|
|
|
private class func isInputImageValidOutputImage(image: UIImage?, imageData: Data?, dataUTI: String!) -> Bool {
|
|
|
|
guard let image = image else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
guard let imageData = imageData else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if dataUTI == kUTTypeJPEG as String {
|
|
|
|
let imageUploadQuality = Environment.preferences().imageUploadQuality()
|
|
|
|
let maxSize = maxSizeForImage(image: image, imageUploadQuality:imageUploadQuality)
|
|
|
|
if image.size.width <= maxSize &&
|
|
|
|
image.size.height <= maxSize &&
|
|
|
|
imageData.count <= kMaxFileSizeImage {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Factory method for an image attachment.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may nil or not be valid.
|
|
|
|
// Check the attachment's error property.
|
2017-03-10 21:09:37 +01:00
|
|
|
public class func imageAttachment(withImage image: UIImage?, dataUTI: String!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(dataUTI.characters.count > 0)
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
guard let image = image else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Make a placeholder attachment on which to hang errors if necessary.
|
|
|
|
let attachment = SignalAttachment(data : Data(), dataUTI: dataUTI)
|
2017-03-10 21:09:37 +01:00
|
|
|
attachment.image = image
|
|
|
|
|
|
|
|
Logger.verbose("\(TAG) Writing \(attachment.mimeType()) as image/jpeg")
|
|
|
|
return compressImageAsJPEG(image : image, attachment : attachment)
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 21:09:37 +01:00
|
|
|
private class func compressImageAsJPEG(image: UIImage!, attachment: SignalAttachment!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(attachment.error == nil)
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
var imageUploadQuality = Environment.preferences().imageUploadQuality()
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
while true {
|
|
|
|
let maxSize = maxSizeForImage(image: image, imageUploadQuality:imageUploadQuality)
|
2017-03-10 21:09:37 +01:00
|
|
|
var dstImage: UIImage! = image
|
|
|
|
if image.size.width > maxSize ||
|
|
|
|
image.size.height > maxSize {
|
|
|
|
dstImage = imageScaled(image, toMaxSize: maxSize)
|
|
|
|
}
|
|
|
|
guard let jpgImageData = UIImageJPEGRepresentation(dstImage,
|
2017-03-10 14:46:36 +01:00
|
|
|
jpegCompressionQuality(imageUploadQuality:imageUploadQuality)) else {
|
|
|
|
attachment.error = .couldNotConvertToJpeg
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
if jpgImageData.count <= kMaxFileSizeImage {
|
|
|
|
let recompressedAttachment = SignalAttachment(data : jpgImageData, dataUTI: kUTTypeJPEG as String)
|
|
|
|
recompressedAttachment.image = dstImage
|
|
|
|
return recompressedAttachment
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// If the JPEG output is larger than the file size limit,
|
|
|
|
// continue to try again by progressively reducing the
|
|
|
|
// image upload quality.
|
|
|
|
switch imageUploadQuality {
|
|
|
|
case .uncropped:
|
|
|
|
imageUploadQuality = .high
|
|
|
|
case .high:
|
|
|
|
imageUploadQuality = .medium
|
|
|
|
case .medium:
|
|
|
|
imageUploadQuality = .low
|
|
|
|
case .low:
|
|
|
|
attachment.error = .fileSizeTooLarge
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
private class func imageScaled(_ image: UIImage, toMaxSize size: CGFloat) -> UIImage {
|
|
|
|
var scaleFactor: CGFloat
|
|
|
|
let aspectRatio: CGFloat = image.size.height / image.size.width
|
|
|
|
if aspectRatio > 1 {
|
|
|
|
scaleFactor = size / image.size.width
|
2017-03-10 21:09:37 +01:00
|
|
|
} else {
|
2017-03-10 14:46:36 +01:00
|
|
|
scaleFactor = size / image.size.height
|
|
|
|
}
|
|
|
|
let newSize = CGSize(width: CGFloat(image.size.width * scaleFactor), height: CGFloat(image.size.height * scaleFactor))
|
|
|
|
UIGraphicsBeginImageContext(newSize)
|
|
|
|
image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(newSize.width), height: CGFloat(newSize.height)))
|
|
|
|
let updatedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
|
|
|
|
UIGraphicsEndImageContext()
|
|
|
|
return updatedImage!
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
private class func maxSizeForImage(image: UIImage, imageUploadQuality: TSImageQuality) -> CGFloat {
|
|
|
|
switch imageUploadQuality {
|
|
|
|
case .uncropped:
|
|
|
|
return max(image.size.width, image.size.height)
|
|
|
|
case .high:
|
|
|
|
return 2048
|
|
|
|
case .medium:
|
|
|
|
return 1024
|
|
|
|
case .low:
|
|
|
|
return 512
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
private class func jpegCompressionQuality(imageUploadQuality: TSImageQuality) -> CGFloat {
|
|
|
|
switch imageUploadQuality {
|
|
|
|
case .uncropped:
|
|
|
|
return 1
|
|
|
|
case .high:
|
|
|
|
return 0.9
|
|
|
|
case .medium:
|
|
|
|
return 0.5
|
|
|
|
case .low:
|
|
|
|
return 0.3
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Video Attachments
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Factory method for video attachments.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may not be valid.
|
|
|
|
// Check the attachment's error property.
|
2017-03-10 21:09:37 +01:00
|
|
|
public class func videoAttachment(withData data: Data?, dataUTI: String!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
return newAttachment(withData : data,
|
|
|
|
dataUTI : dataUTI,
|
|
|
|
validUTISet : videoUTISet(),
|
2017-03-10 21:09:37 +01:00
|
|
|
maxFileSize : kMaxFileSizeVideo)
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Audio Attachments
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Factory method for audio attachments.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may not be valid.
|
|
|
|
// Check the attachment's error property.
|
2017-03-10 21:09:37 +01:00
|
|
|
public class func audioAttachment(withData data: Data?, dataUTI: String!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
return newAttachment(withData : data,
|
|
|
|
dataUTI : dataUTI,
|
|
|
|
validUTISet : audioUTISet(),
|
2017-03-10 21:09:37 +01:00
|
|
|
maxFileSize : kMaxFileSizeAudio)
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Generic Attachments
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Factory method for generic attachments.
|
|
|
|
//
|
|
|
|
// NOTE: The attachment returned by this method may not be valid.
|
|
|
|
// Check the attachment's error property.
|
2017-03-10 21:09:37 +01:00
|
|
|
public class func genericAttachment(withData data: Data?, dataUTI: String!) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
return newAttachment(withData : data,
|
|
|
|
dataUTI : dataUTI,
|
|
|
|
validUTISet : nil,
|
2017-03-10 21:09:37 +01:00
|
|
|
maxFileSize : kMaxFileSizeGeneric)
|
2017-03-10 14:46:36 +01:00
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// MARK: Helper Methods
|
2017-03-10 21:09:37 +01:00
|
|
|
|
|
|
|
private class func newAttachment(withData data: Data?,
|
|
|
|
dataUTI: String!,
|
|
|
|
validUTISet: Set<String>?,
|
|
|
|
maxFileSize: Int) -> SignalAttachment! {
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(dataUTI.characters.count > 0)
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
assert(data != nil)
|
|
|
|
guard let data = data else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
let attachment = SignalAttachment(data : data, dataUTI: dataUTI)
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
if validUTISet != nil {
|
|
|
|
guard validUTISet!.contains(dataUTI) else {
|
|
|
|
attachment.error = .invalidFileFormat
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
guard data.count > 0 else {
|
|
|
|
assert(data.count > 0)
|
|
|
|
attachment.error = .invalidData
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
guard data.count <= maxFileSize else {
|
|
|
|
attachment.error = .fileSizeTooLarge
|
|
|
|
return attachment
|
|
|
|
}
|
2017-03-10 21:09:37 +01:00
|
|
|
|
2017-03-10 14:46:36 +01:00
|
|
|
// Attachment is valid
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
}
|