// 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.WriteChannel;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import org.springframework.context.ApplicationEventPublisher;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
/*
 * 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-08.
 */
public class UploadTask implements Runnable {

  private final TaskInfo taskInfo;
  private final ApplicationEventPublisher applicationEventPublisher;

  public UploadTask(TaskInfo taskInfo,
                    ApplicationEventPublisher applicationEventPublisher) {
    this.taskInfo = taskInfo;
    this.applicationEventPublisher = applicationEventPublisher;
  }

  @Override
  public void run() {
    if(taskInfo.isCancelling()) {
      System.out.println(">> >> CANCELLING upload for file = " + taskInfo.getFile());
    }
    if (taskInfo.getStatus() != TaskStatus.WAITING && taskInfo.getStatus() != TaskStatus.QUEUED) {
      return;
    }
    taskInfo.setStatus(TaskStatus.RUNNING);
    taskInfo.setStartTime(System.currentTimeMillis());
    AnalyticsMessage m1 = AnalyticsMessage.from(taskInfo, AnalyticsMessage.Event.SINGLE_FILE_START, "Started Single 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/UploadTask.java
import com.google.ce.media.contentuploader.message.*;
import com.google.ce.media.contentuploader.utils.DateUtils;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import org.springframework.context.ApplicationEventPublisher;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;

/*
 * 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-08.
 */
public class UploadTask implements Runnable {

  private final TaskInfo taskInfo;
  private final ApplicationEventPublisher applicationEventPublisher;

  public UploadTask(TaskInfo taskInfo,
                    ApplicationEventPublisher applicationEventPublisher) {
    this.taskInfo = taskInfo;
    this.applicationEventPublisher = applicationEventPublisher;
  }

  @Override
  public void run() {
    if(taskInfo.isCancelling()) {
      System.out.println(">> >> CANCELLING upload for file = " + taskInfo.getFile());
    }
    if (taskInfo.getStatus() != TaskStatus.WAITING && taskInfo.getStatus() != TaskStatus.QUEUED) {
      return;
    }
    taskInfo.setStatus(TaskStatus.RUNNING);
    taskInfo.setStartTime(System.currentTimeMillis());
    AnalyticsMessage m1 = AnalyticsMessage.from(taskInfo, AnalyticsMessage.Event.SINGLE_FILE_START, "Started Single Upload");
    m1.setStartTime(DateUtils.time(taskInfo.getStartTime()));