|
|
@ -603,7 +603,8 @@ public class SignalAttachment: NSObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attachment.cachedImage = image
|
|
|
|
attachment.cachedImage = image
|
|
|
|
|
|
|
|
|
|
|
|
if isValidOutputImage(image: image, dataSource: dataSource, dataUTI: dataUTI, imageQuality:imageQuality) {
|
|
|
|
let isValidOutput = isValidOutputImage(image: image, dataSource: dataSource, dataUTI: dataUTI, imageQuality: imageQuality)
|
|
|
|
|
|
|
|
|
|
|
|
if let sourceFilename = dataSource.sourceFilename,
|
|
|
|
if let sourceFilename = dataSource.sourceFilename,
|
|
|
|
let sourceFileExtension = sourceFilename.fileExtension,
|
|
|
|
let sourceFileExtension = sourceFilename.fileExtension,
|
|
|
|
["heic", "heif"].contains(sourceFileExtension.lowercased()) {
|
|
|
|
["heic", "heif"].contains(sourceFileExtension.lowercased()) {
|
|
|
@ -616,26 +617,27 @@ public class SignalAttachment: NSObject {
|
|
|
|
// updating the extension as well. No problem.
|
|
|
|
// updating the extension as well. No problem.
|
|
|
|
// However the problem comes in when you edit an HEIC image in Photos.app - the image is saved
|
|
|
|
// However the problem comes in when you edit an HEIC image in Photos.app - the image is saved
|
|
|
|
// in the Photos.app as a JPEG, but retains the (now incongruous) HEIC extension in the filename.
|
|
|
|
// in the Photos.app as a JPEG, but retains the (now incongruous) HEIC extension in the filename.
|
|
|
|
assert(dataUTI == kUTTypeJPEG as String)
|
|
|
|
assert(dataUTI == kUTTypeJPEG as String || !isValidOutput)
|
|
|
|
Logger.verbose("\(self.TAG) changing extension: \(sourceFileExtension) to match jpg uti type")
|
|
|
|
Logger.verbose("\(self.TAG) changing extension: \(sourceFileExtension) to match jpg uti type")
|
|
|
|
|
|
|
|
|
|
|
|
let baseFilename = sourceFilename.filenameWithoutExtension
|
|
|
|
let baseFilename = sourceFilename.filenameWithoutExtension
|
|
|
|
dataSource.sourceFilename = baseFilename.appendingFileExtension("jpg")
|
|
|
|
dataSource.sourceFilename = baseFilename.appendingFileExtension("jpg")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if isValidOutput {
|
|
|
|
Logger.verbose("\(TAG) Sending raw \(attachment.mimeType)")
|
|
|
|
Logger.verbose("\(TAG) Sending raw \(attachment.mimeType)")
|
|
|
|
return attachment
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
Logger.verbose("\(TAG) Compressing attachment as image/jpeg, \(dataSource.dataLength()) bytes")
|
|
|
|
Logger.verbose("\(TAG) Compressing attachment as image/jpeg, \(dataSource.dataLength()) bytes")
|
|
|
|
return compressImageAsJPEG(image: image, attachment: attachment, filename: dataSource.sourceFilename, imageQuality: imageQuality)
|
|
|
|
return compressImageAsJPEG(image: image, attachment: attachment, filename: dataSource.sourceFilename, imageQuality: imageQuality)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the proposed attachment already conforms to the
|
|
|
|
// If the proposed attachment already conforms to the
|
|
|
|
// file size and content size limits, don't recompress it.
|
|
|
|
// file size and content size limits, don't recompress it.
|
|
|
|
private class func isValidOutputImage(image: UIImage?, dataSource: DataSource?, dataUTI: String, imageQuality: TSImageQuality) -> Bool {
|
|
|
|
private class func isValidOutputImage(image: UIImage?, dataSource: DataSource?, dataUTI: String, imageQuality: TSImageQuality) -> Bool {
|
|
|
|
guard let image = image else {
|
|
|
|
guard image != nil else {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
guard let dataSource = dataSource else {
|
|
|
|
guard let dataSource = dataSource else {
|
|
|
@ -644,7 +646,6 @@ public class SignalAttachment: NSObject {
|
|
|
|
guard SignalAttachment.outputImageUTISet.contains(dataUTI) else {
|
|
|
|
guard SignalAttachment.outputImageUTISet.contains(dataUTI) else {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if doesImageHaveAcceptableFileSize(dataSource: dataSource, imageQuality: imageQuality) &&
|
|
|
|
if doesImageHaveAcceptableFileSize(dataSource: dataSource, imageQuality: imageQuality) &&
|
|
|
|
dataSource.dataLength() <= kMaxFileSizeImage {
|
|
|
|
dataSource.dataLength() <= kMaxFileSizeImage {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
@ -682,7 +683,8 @@ public class SignalAttachment: NSObject {
|
|
|
|
assert(attachment.error == nil)
|
|
|
|
assert(attachment.error == nil)
|
|
|
|
|
|
|
|
|
|
|
|
if imageQuality == .original &&
|
|
|
|
if imageQuality == .original &&
|
|
|
|
attachment.dataLength < kMaxFileSizeGeneric {
|
|
|
|
attachment.dataLength < kMaxFileSizeGeneric &&
|
|
|
|
|
|
|
|
outputImageUTISet.contains(attachment.dataUTI) {
|
|
|
|
// We should avoid resizing images attached "as documents" if possible.
|
|
|
|
// We should avoid resizing images attached "as documents" if possible.
|
|
|
|
return attachment
|
|
|
|
return attachment
|
|
|
|
}
|
|
|
|
}
|
|
|
|