mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 12:44:51 +00:00
115 lines
2.9 KiB
Swift
115 lines
2.9 KiB
Swift
//
|
|
// Sticker.swift
|
|
// StickerSlack
|
|
//
|
|
// Created by neon443 on 06/03/2026.
|
|
//
|
|
|
|
import Foundation
|
|
import Messages
|
|
|
|
extension StickerProtocol {
|
|
var localImageURLString: String {
|
|
let containerPath: String
|
|
switch type {
|
|
case .slackEmoji:
|
|
containerPath = EmojiHoarder.container.safePath
|
|
case .giphyGifs:
|
|
containerPath = GifHoarder.container.safePath
|
|
}
|
|
let urlString = remoteImageURL.absoluteString
|
|
let split = urlString.split(separator: ".")
|
|
let fileExtension = ".\(split.last!)"
|
|
return containerPath+name+"."+id+fileExtension
|
|
}
|
|
|
|
var localImageURL: URL {
|
|
if #available(iOS 16, *) {
|
|
return URL(filePath: localImageURLString)
|
|
} else {
|
|
return URL(fileURLWithPath: localImageURLString)
|
|
}
|
|
}
|
|
|
|
var msSticker: MSSticker? {
|
|
guard FileManager.default.fileExists(atPath: localImageURLString) else {
|
|
return nil
|
|
}
|
|
return try? MSSticker(contentsOfFileURL: localImageURL, localizedDescription: name)
|
|
}
|
|
|
|
var isLocal: Bool {
|
|
return FileManager.default.fileExists(atPath: localImageURLString)
|
|
}
|
|
|
|
var image: UIImage? {
|
|
if FileManager.default.fileExists(atPath: localImageURLString),
|
|
let data = try? Data(contentsOf: localImageURL),
|
|
let img = UIImage(data: data) {
|
|
return img
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var type: StickerType {
|
|
let typeOfSelf = Swift.type(of: self)
|
|
if typeOfSelf == Emoji.self {
|
|
return .slackEmoji
|
|
} else if typeOfSelf == Gif.self {
|
|
return .giphyGifs
|
|
} else {
|
|
fatalError("unrecognised sticker type get crashed lmfao")
|
|
}
|
|
}
|
|
|
|
nonisolated
|
|
func downloadImage() async throws {
|
|
if let data = try? await Data(contentsOf: localImageURL),
|
|
let _ = UIImage(data: data) {
|
|
return
|
|
}
|
|
|
|
var (data, _) = try await URLSession.shared.data(from: remoteImageURL)
|
|
|
|
let targetSize = CGSize(width: 600, height: 600)
|
|
if let uiImage = UIImage(data: data),
|
|
await localImageURLString.suffix(4) != ".gif" {
|
|
data = await resize(image: uiImage, to: targetSize).pngData()!
|
|
}
|
|
|
|
try! await data.write(to: localImageURL)
|
|
}
|
|
|
|
func deleteImage() {
|
|
try? FileManager.default.removeItem(at: localImageURL)
|
|
return
|
|
}
|
|
|
|
nonisolated func resize(image: UIImage, to targetSize: CGSize) async -> UIImage {
|
|
let renderer = UIGraphicsImageRenderer(size: targetSize)
|
|
let resizedImage = renderer.image { _ in
|
|
image.draw(in: CGRect(origin: .zero, size: targetSize))
|
|
}
|
|
return resizedImage
|
|
}
|
|
|
|
nonisolated func resize(cgImage: CGImage, to targetSize: CGSize) -> CGImage? {
|
|
let width = Int(targetSize.width)
|
|
let height = Int(targetSize.height)
|
|
guard let context = CGContext(
|
|
data: nil,
|
|
width: width,
|
|
height: height,
|
|
bitsPerComponent: cgImage.bitsPerComponent,
|
|
bytesPerRow: cgImage.bytesPerRow,
|
|
space: cgImage.colorSpace ?? CGColorSpaceCreateDeviceRGB(),
|
|
bitmapInfo: cgImage.bitmapInfo
|
|
) else { fatalError() }
|
|
|
|
context.interpolationQuality = .default
|
|
context.draw(cgImage, in: CGRect(origin: .zero, size: targetSize))
|
|
return context.makeImage()
|
|
}
|
|
}
|