Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding the convert_file function to the file manager.

  • Loading branch information...
commit a656fcb38748054f5810bee7017c9bf1b364e971 1 parent 62c5daf
authored mudrd8mz committed
90  backup/util/helper/convert_file_manager.class.php
... ...
@@ -0,0 +1,90 @@
  1
+<?php
  2
+
  3
+abstract class convert_file_manager {
  4
+    public static function convert_course_files() {
  5
+    }
  6
+    
  7
+    public static function convert_file($converter, $filepath, $mod_context) {
  8
+        global $DB;
  9
+
  10
+        // make a dummy record in the temp tabke to get auto gen FILEID
  11
+        $backupid = $converter->get_id();
  12
+        $dummy = new stdClass;
  13
+        $dummy->backupid = $backupid;
  14
+        $dummy->itemname = random_string(32);
  15
+        if(!$fileid = $DB->insert_record('backup_ids_temp', $dummy)) {
  16
+            // Replace with a converter_exception?
  17
+            $info = convert_helper::obj_to_readable($dummy);
  18
+            throw new Exception(sprintf("Could not insert dummy record with info: (%s)", $info));
  19
+        }
  20
+        
  21
+        // TODO: make this look at moddata as well
  22
+        $converter_path = $converter->get_convertdir();
  23
+        $oldpath = $converter->get_tempdir();
  24
+        $converted_course_files = "$oldpath/course_files";
  25
+
  26
+        // Start processing the file now
  27
+        $pathinfo = pathinfo($filepath);
  28
+        // Try to get the base
  29
+        $base = str_replace($converted_course_files, '', $pathinfo['dirname']);
  30
+        // if the $base is the same as before, then it is root
  31
+        if($base == $pathinfo['dirname']) {
  32
+            $path = '/';
  33
+        } else if(is_dir($filepath)) {
  34
+            $path = '/'. $pathinfo['basename'] . '/';
  35
+        } else {
  36
+            $path = $base . '/';
  37
+        }  
  38
+
  39
+        // TODO: Might need to spoof more fields
  40
+        $file_data = new stdClass;
  41
+        $file_data->id = $fileid;
  42
+        $file_data->contenthash = sha1_file($filepath);
  43
+        $file_data->mod_context = $mod_context;
  44
+        $file_data->filename = is_dir($filepath) ? '.' : $pathinfo['basename'];
  45
+        $file_data->filesize = is_dir($filepath) ? '0' : filesize($filepath);
  46
+        $file_data->mimetype = is_dir($filepath) ? '$@NULL@$' : mimeinfo('type', $pathinfo['basename']);
  47
+        $file_data->created = time();
  48
+        $file_data->modified = time();
  49
+
  50
+        // Move in-memory file data to the real deal on the server
  51
+        $hashdir = substr($file_data->contenthash, 0, 2);
  52
+        $folders = array(
  53
+                    "$converted_path/files",
  54
+                    "$converted_path/files/$hashdir"
  55
+                  );
  56
+        if(!array_reduce($folders, function($in, $folder) {
  57
+            return $in and (file_exists($folder) or mkdir($folder));
  58
+        }, true)) {
  59
+            throw new Exception("Could not create temp dirs for file");
  60
+        }
  61
+        // Move file
  62
+        $new_path = $folders[1]."/$file_data->contenthash";
  63
+        if(!rename($filepath, $new_path) {
  64
+            throw new Exception(sprintf("Could not move file into %s", $new_path));
  65
+        }       
  66
+
  67
+        // Time to insert this record now
  68
+        $fs_record = new stdClass;
  69
+        $fs_record->id = $fileid;
  70
+        $fs_record->backupid = $backupid;
  71
+        $fs_ercord->itemname = 'file';
  72
+        $fs_record->itemid = $fileid
  73
+        $fs_record->info = serialize($file_data);
  74
+        if(!$DB->update_record('backup_ids_temp', $fs_record)) {
  75
+            $info = convert_helper::obj_to_readable($fs_record);
  76
+            throw new Exception(sprintf("Could not update with real info: (%s)", $info));
  77
+        }
  78
+
  79
+        // TODO: register inforef
  80
+        return true;
  81
+    }
  82
+
  83
+    public static function get_temp_file_info($info) {
  84
+        global $DB;
  85
+        
  86
+    }
  87
+    
  88
+    public static function create_files_xml() {
  89
+    }
  90
+}

0 notes on commit a656fcb

Please sign in to comment.
Something went wrong with that request. Please try again.