<?php

defined("ABSPATH") or die("");

class DUP_PRO_System_Checker
{
    const SYSTEM_CHECK_PERIOD_IN_SEC = 86400;  // 24 hours

    public static function check()
    {
        $global = DUP_PRO_Global_Entity::get_instance();
        $time   = time();

        if (($time - $global->last_system_check_timestamp) >= self::SYSTEM_CHECK_PERIOD_IN_SEC) {
            DUP_PRO_Log::trace("Doing system checker check because time = $time and last timestamp = {$global->last_system_check_timestamp}");

            try {
                self::purgeOldS3MultipartUploads();
            } catch (Exception $ex) {
                DUP_PRO_Log::trace("Got exception during s3 system check: " . $ex->getMessage());
            }

            DUP_PRO_Package_Importer::purgeOldImports();

            $global->last_system_check_timestamp = time();
            $global->save();
        }
    }

    private static function purgeOldS3MultipartUploads()
    {
        $storages = DUP_PRO_Storage_Entity::get_all();

        foreach ($storages as $storage) {
            if ($storage->storage_type == DUP_PRO_Storage_Types::S3) {
                $s3_client = $storage->get_full_s3_client();

                $active_uploads = DUP_PRO_S3_U::get_active_multipart_uploads($s3_client, $storage->s3_bucket, $storage->s3_storage_folder);

                if (($active_uploads != null) && is_array($active_uploads)) {
                    foreach ($active_uploads as $active_upload) {
                        // Needs to be at least 48 hours old - don't want to much around with timezone so this is safe
                        $time_delta = time() - $active_upload->timestamp;

                        if ($time_delta > (48 * 3600)) {
                            DUP_PRO_Log::trace("Aborting upload because timestamp = {$active_upload->timestamp} while time is " . time());
                            DUP_PRO_S3_U::abort_multipart_upload($s3_client, $storage->s3_bucket, $active_upload->key, $active_upload->upload_id);
                        }
                    }
                }
            }
        }
    }
}
