From 06e008ed6346b1829d3264306dfdce80004a12f8 Mon Sep 17 00:00:00 2001
From: ajstanley <alanjarlathstanley@gmail.com>
Date: Thu, 20 Mar 2025 15:12:16 +0000
Subject: [PATCH] File types added

---
 src/Form/CreateMediaFromFileForm.php |  3 +--
 src/Utils.php                        | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/Form/CreateMediaFromFileForm.php b/src/Form/CreateMediaFromFileForm.php
index af6d221..66435e6 100644
--- a/src/Form/CreateMediaFromFileForm.php
+++ b/src/Form/CreateMediaFromFileForm.php
@@ -142,8 +142,7 @@ final class CreateMediaFromFileForm extends FormBase {
     $destination = "{$file_system}{$directory}";
     $media_type = $form_state->getValue('media_type');
     $media_use = $form_state->getValue('media_use');
-    // $results = $this->mediaUtils->buildMedia($source_path, $destination, $media_type, $media_use);
-    $results = $this->mediaUtils->buildMediaBatch($source_path, $destination, $media_type, $media_use);
+    $this->mediaUtils->buildMediaBatch($source_path, $destination, $media_type, $media_use);
     $this->messenger()->addStatus($this->t('Media files have been processed from %source to %destination.', [
       '%source' => $source_path,
       '%destination' => $destination,
diff --git a/src/Utils.php b/src/Utils.php
index 4281d74..1dcae55 100644
--- a/src/Utils.php
+++ b/src/Utils.php
@@ -20,6 +20,14 @@ class Utils {
     if (!isset($context['results'])) {
       $context['results'] = [];
     }
+    $file_types = [
+      'audio' => 'field_media_audio_file',
+      'document' => 'field_media_document',
+      'file' => 'field_media_file',
+      'image' => 'field_media_image',
+      'video' => 'field_media_video_file',
+    ];
+    $file_type = $file_types[$media_type] ?? 'field_media_file';
 
     $logger = \Drupal::logger('islandora_inplace_media');
     $fileSystem = \Drupal::service('file_system');
@@ -27,12 +35,15 @@ class Utils {
     $source_path = $dir . '/' . $file_name;
     $destination_path = $dest . '/' . $file_name;
 
-    if (!file_exists($source_path)) {
+    if (!\file_exists($source_path)) {
       $logger->warning('File does not exist: @file', ['@file' => $source_path]);
       return;
     }
 
     $moved_file = $fileSystem->move($source_path, $destination_path, FileSystemInterface::EXISTS_RENAME);
+    $absolute_path = $fileSystem->realpath($destination_path);
+    chown($absolute_path, 'www-data');
+    chgrp($absolute_path, 'lib-dev');
     $new_file = File::create([
       'uri' => $moved_file,
       'status' => 1,
@@ -43,13 +54,12 @@ class Utils {
     if (preg_match('/^(\d+)_/', $file_name, $matches)) {
       $nid = $matches[1] ?? NULL;
     }
-    unlink($source_path);
 
     if ($nid) {
       $media = Media::create([
         "bundle" => $media_type,
         "name" => $file_name,
-        "field_media_file" => [
+        $file_type => [
           "target_id" => $new_file->id(),
         ],
         'field_media_use' => [