createThumbnailUploadPdfFile method

Future<T> createThumbnailUploadPdfFile(
  1. String memberMediumDocumentID,
  2. String filePath,
  3. String documentID, {
  4. PdfAvailable? feedbackFunction,
  5. FeedbackProgress? feedbackProgress,
})

Implementation

Future<T> createThumbnailUploadPdfFile(
    String memberMediumDocumentID, String filePath, String documentID,
    {PdfAvailable? feedbackFunction,
    FeedbackProgress? feedbackProgress}) async {
  print('createThumbnailUploadPdfFile');
  // First, upload the file
  final document = await PdfDocument.openFile(filePath);
  final pageCount = document.pagesCount;
  var taskCounter = 1;
  var totalTasks = 1 + (pageCount * 4);
  var baseName = context.basenameWithoutExtension(filePath);

  // Now create extra MemberImageModels for each page
  dynamic previousMediumId;
  String newDocumentID;
  T? returnMe;
  for (var i = pageCount; i >= 1; i--) {
    newDocumentID = '$documentID-$i';
    var newBaseName = '$baseName-$i';

    // First, create the image and thumbnail
    var pageData = await MediumData.createPhotoWithThumbnailFromPdfPage(
        filePath, newBaseName, i);
    taskCounter++;
    _feedBackAggregatedProgress(taskCounter, totalTasks, 1,
        feedbackProgress: feedbackProgress);

    // Second, upload the thumbnail;
    taskCounter++;
    var pageThumbnail = await UploadInfo.uploadData(
        pageData.thumbNailData.baseName,
        pageData.thumbNailData.data,
        app.documentID,
        ownerId,
        packageName,
        readAccessCustomMetaData(),
        feedbackProgress: (progress) => _feedBackAggregatedProgress(
            taskCounter, totalTasks, progress,
            feedbackProgress: feedbackProgress));

    // Third, upload the image
    taskCounter++;
    var pageImage = await UploadInfo.uploadData(
        pageData.photoData.baseName,
        pageData.photoData.data,
        app.documentID,
        ownerId,
        packageName,
        readAccessCustomMetaData(),
        feedbackProgress: (progress) => _feedBackAggregatedProgress(
            taskCounter, totalTasks, progress,
            feedbackProgress: feedbackProgress));

    // Create the MediumModel representation
    var newMediumModel = await constructMediumModel(
        newDocumentID,
        newBaseName,
        pageImage,
        pageThumbnail,
        pageData,
        AbstractMediumType.photo,
        previousMediumId);
    returnMe = newMediumModel;

    previousMediumId = newDocumentID;
    taskCounter++;
    _feedBackAggregatedProgress(taskCounter, totalTasks, 1,
        feedbackProgress: feedbackProgress);
  }
  if (feedbackFunction != null) {
    feedbackFunction(returnMe);
  }
  if (returnMe != null) {
    return returnMe;
  } else {
    throw Exception("No page found");
  }
}