uploadFile method
Upload a file to Slack using the three-step external upload process
Implementation
Future<bool> uploadFile({
required File file,
String? title,
String? comment,
}) async {
try {
_debugLog('Starting file upload: ${file.path}');
// Step 1: Get upload URL and file ID from Slack
final uploadInfo = await _getUploadUrl(file);
if (uploadInfo == null) {
Logger.warning('Failed to obtain upload URL from Slack.');
return false;
}
_debugLog('Got upload URL and file ID');
// Step 2: Upload file to the provided URL
final uploadSuccess = await _uploadToUrl(file, uploadInfo);
if (!uploadSuccess) {
Logger.warning('Failed to upload file content to Slack S3 URL.');
return false;
}
_debugLog('File uploaded to URL successfully');
// Step 3: Complete the upload and post to channel
final success = await _completeUpload(
fileId: uploadInfo['file_id'],
title: title,
comment: comment,
);
if (success) {
Logger.success('File uploaded to Slack successfully.');
} else {
Logger.warning('Failed to complete file upload on Slack.');
}
return success;
} catch (e) {
_debugLog('Exception during file upload: $e');
Logger.warning('Failed to upload file to Slack: $e');
return false;
}
}