// Path: app-desktop/src/main/java/com/google/ce/media/contentuploader/utils/DateUtils.java
// public class DateUtils {
//   private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZoneUTC();
// 
//   public static String now() {
//     return formatter.print(System.currentTimeMillis());
//   }
// 
//   public static String time(long millis) {
//     return formatter.print(millis);
//   }
// }


import com.google.ce.media.contentuploader.message.*;
import com.google.ce.media.contentuploader.utils.DateUtils;
import com.google.cloud.storage.BlobInfo;
import org.apache.commons.codec.digest.PureJavaCrc32C;
import org.springframework.context.ApplicationEventPublisher;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.google.ce.media.contentuploader.exec;



/**
 * Created in gcs-uploader on 2020-01-23.
 */
public class CompositeUploadTask implements Runnable {
  private final TaskInfo taskInfo;
  private final UploadTaskPool uploadTaskPool;
  //  private final Hasher md5HasherTotal = md5Digest.newHasher();
//  private final Hasher crcHasherTotal = crcDigest.newHasher();
  private final PureJavaCrc32C crc32C = new PureJavaCrc32C();

  public CompositeUploadTask(TaskInfo taskInfo,
                             UploadTaskPool uploadTaskPool,
                             ApplicationEventPublisher applicationEventPublisher) throws NoSuchAlgorithmException {
    this.taskInfo = taskInfo;
    this.uploadTaskPool = uploadTaskPool;
  }

  @Override
  public void run() {
    if (taskInfo.getStatus() != TaskStatus.WAITING && taskInfo.getStatus() != TaskStatus.QUEUED) {
      return;
    }
    try {
      Long startTime = System.currentTimeMillis();

      taskInfo.setStatus(TaskStatus.RUNNING);
      taskInfo.setStartTime(startTime);

      AnalyticsMessage m1 = AnalyticsMessage.from(taskInfo, AnalyticsMessage.Event.COMPOSITE_FILE_START, "Started Composite Upload");
// Path: app-desktop/src/main/java/com/google/ce/media/contentuploader/utils/DateUtils.java
// public class DateUtils {
//   private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZoneUTC();
// 
//   public static String now() {
//     return formatter.print(System.currentTimeMillis());
//   }
// 
//   public static String time(long millis) {
//     return formatter.print(millis);
//   }
// }


// Path: app-desktop/src/main/java/com/google/ce/media/contentuploader/exec/CompositeUploadTask.java
import com.google.ce.media.contentuploader.message.*;
import com.google.ce.media.contentuploader.utils.DateUtils;
import com.google.cloud.storage.BlobInfo;
import org.apache.commons.codec.digest.PureJavaCrc32C;
import org.springframework.context.ApplicationEventPublisher;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.google.ce.media.contentuploader.exec;



/**
 * Created in gcs-uploader on 2020-01-23.
 */
public class CompositeUploadTask implements Runnable {
  private final TaskInfo taskInfo;
  private final UploadTaskPool uploadTaskPool;
  //  private final Hasher md5HasherTotal = md5Digest.newHasher();
//  private final Hasher crcHasherTotal = crcDigest.newHasher();
  private final PureJavaCrc32C crc32C = new PureJavaCrc32C();

  public CompositeUploadTask(TaskInfo taskInfo,
                             UploadTaskPool uploadTaskPool,
                             ApplicationEventPublisher applicationEventPublisher) throws NoSuchAlgorithmException {
    this.taskInfo = taskInfo;
    this.uploadTaskPool = uploadTaskPool;
  }

  @Override
  public void run() {
    if (taskInfo.getStatus() != TaskStatus.WAITING && taskInfo.getStatus() != TaskStatus.QUEUED) {
      return;
    }
    try {
      Long startTime = System.currentTimeMillis();

      taskInfo.setStatus(TaskStatus.RUNNING);
      taskInfo.setStartTime(startTime);

      AnalyticsMessage m1 = AnalyticsMessage.from(taskInfo, AnalyticsMessage.Event.COMPOSITE_FILE_START, "Started Composite Upload");
      m1.setStartTime(DateUtils.time(startTime));