How to (properly) deal with local Upload state?

Hi there!

Noob question, how should I deal with the Local Upload widget state in a custom action?

I'm using the widget to upload a video locally, and I would like to access it (to get duration and thumbnail, for example). My code works when downloading a video from a URL, but I struggle to use the local upload directly.

I've ended up trying some weird things that don't work yet, but I was wondering if there was a cleaner and proper way to deal with this.

Thank you for your guidance,

import 'package:media_info/media_info.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

Future<int> videoDurationCheck(
  FFUploadedFile videoToCheck,
) async {
  final MediaInfo _mediaInfo = MediaInfo();

  try {
    // Get the temp folder
    final Directory tempDir = await getTemporaryDirectory();

    // New File in temp
    final File tempFile = File('${tempDir.path}/${videoToCheck.name}');

    // Write the bytes to new instance
    await tempFile.writeAsBytes(videoToCheck.bytes!);

    // Get media information
    final Map<String, dynamic> mediaInfo =
        await _mediaInfo.getMediaInfo(tempFile.path);
    final int duration =
        mediaInfo['duration'] != null ? (mediaInfo['duration'] as int) : 0;

    // Delete the temp file
    await tempFile.delete();

    return duration;
  } catch (e) {
    print('Error getting video duration: $e');
    return 0;
  }
}

2
3 replies