Other Functions

The following functions are available globally.

Username Validation

  • Three differernt POST structs contain username fields; this fn exists to ensure they all validate the username the same way. This fn is designed to return a list of validation failure strings–if it returns an empty array the username is valid.

    Declaration

    Swift

    private func usernameValidations(username: String) -> [String]
  • Returns the file system path for the given image filename. Makes sure all image directories in the path exist.

    Currently, this fn returns paths in the form: /images///.jpg where “xx” is the first 2 characters of the filename.

    Declaration

    Swift

    func getImagePath(for image: String, format: String? = nil, usage: ImageUsage, size: ImageSizeGroup, on req: Request) throws -> URL
  • Returns true when the image data is a format that can carry animation (GIF or WebP).

    Declaration

    Swift

    func isAnimatableFormat(_ data: Data) -> Bool
  • Detects the file extension for image data. Returns “jpg” for unrecognized formats.

    Declaration

    Swift

    func detectExtension(_ data: Data) -> String
  • Loads an image from data, auto-detecting format and applying EXIF rotation. Alpha is preserved — flattening only happens at JPEG export sites, since PNG/GIF/WebP outputs can carry transparency.

    Declaration

    Swift

    func loadImageFromData(_ data: Data) throws -> SwiftarrImage

    Parameters

    data

    The image data to load

    Return Value

    The loaded image, ready for processing

  • Resizes an image to thumbnail size and encodes it in the requested format. Returns nil if resize fails. JPEG output flattens any alpha onto a white background; PNG/GIF/WebP outputs preserve transparency.

    Declaration

    Swift

    func thumbnailData(from image: SwiftarrImage, format: String) throws -> Data?
  • Creates a thumbnail from an image and writes it to disk in the specified format. Silently skips thumbnail creation if resize fails, matching the original behavior.

    Throws

    Errors from export or file write operations

    Declaration

    Swift

    func createThumbnail(from image: SwiftarrImage, to thumbPath: URL, format: String, on req: Request) throws

    Parameters

    image

    The source image to create a thumbnail from

    thumbPath

    The file path where the thumbnail should be saved

    format

    The output format extension (“jpg”, “png”, etc.)

    req

    The incoming Request, used for logging

Utilities