File Handling

Files and images are represented as an instance of AmityImageData and AmityFileData. This includes accessing file, upload information returned after upload or accessing it from a particular model. For example, accessing uploaded avatar for User through getAvatarInfo would return an instance of AmityImageData.

Instance of AmityFileData or AmityImageData are not meant to be created manually. Use the instance received through AmityFileRepository or other interfaces such as getFileInfo or getImageInfo.

Amity SDK provides various classes and methods for easy handling of files as well as images.

Upload

AmityFileRepository class contains uploadFile and uploadImage methods for uploading files or images respectively. It supports uploading of Binary Data (i.e Data or NSData) as well as uploading file or image from the URL (i.e URL received from iOS FileSystem) directly.

It is the responsibility of the developer to ensure that the file URL can be accessed by the SDK. This may require moving the file or image that you want to upload to some temporary location.

The upload: method also contains a progress block which can be used to determine the progress of the upload.

Upload File

Some APIs such as setFileData when creating file posts requires you to provide the instance of AmityFileData instead of binary Data or URL. In this case, you need to upload the file first using AmityFileRepository and then use the AmityFileData received in completion handler.

// Uploading File from URL
fileRepository.uploadFile(with: <URL>, fileName: "myfile.pdf") { progress in
		// ...
} completion: { fileData, error in
   // Handle completion here
}

The fileName parameter is optional. SDK automatically tries do determine the file name based on the last component of file URL.

Uploading file with Binary (i.e Data) requires an extra additional step of creating an instance of AmityUploadableFile.

// Uploading File Binary

let fileToUpload = AmityUploadableFile(...)
fileRepository.uploadFile(fileToUpload) { progress in
		// ...
} completion: { fileData, error in
		// Handle completion here
}

When file upload is successful, you will receive an instance of AmityFileData which contains information about the uploaded image.

Upload Image

Some APIs such as setAvatar requires you to provide the instance of AmityImageData instead of binary or URL. In this case, you need to upload the file first using AmityFileRepository and then use the AmityImageData received in completion handler.

// Uploading Image
fileRepository.uploadImage(<UIImage>) { progress in
		// Progress in Double
} completion: { imageData, error in
		// Handle completion here
}

// Upload Image from system URL
fileRepository.uploadImage(with: <URL>, isFullImage: false) { progress in
		// ...
} completion: { imageData, error in
		// Handle completion here
}

When image upload is successful, you will receive an instance of AmityImageData which contains information about the uploaded image.

Requirements for Images

  • Supported image types are JPG and PNG.

  • The maximum file size of the image is 1 GB.

Uploading files or images using Binary (i.e UIImage or Data) directly is not memory efficient for large file size. We recommend using upload method instead which accepts the file URL.

Download

AmityFileRepository class also provides a method which helps in downloading of files or images. It supports downloading and providing downloaded file as an instance of UIImage , downloaded file location as URL or Data.

Download file

// Download file directly into filesystem
fileRepository.downloadFile(fromURL: <url>) { downloadLocationUrl, error> in
		// Handle completion
}
        
// Download file & provide it as Data
fileRepository.downloadFileAsData(fromURL: <url>) { data, error> in
		// Handle Completion
}

Download image

// Download image directly into filesystem
fileRepository.downloadImage(fromURL: <url>, size: <AmityMediaSize>) { downloadLocationUrl, error in
		// Handle completion
}

// Download image & convert it into UIImage & provide it
fileRepository.downloadImageAsData(fromURL: <url>, size: <AmityMediaSize>) { image, size, error in
		// Handle completion
}

SDK does not handle caching of any images or files. Developer should handle caching themselves. We recommend using download: method which downloads images or files to the file system and provides URL. This method is memory efficient for downloading large files and images.

Alternatively, User can use their existing implementation or library to handle file or image download directly from the fileUrl present in AmityImageData or AmityFileData.

Last updated