Skip to content

Instantly share code, notes, and snippets.

Created October 14, 2010 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/626240 to your computer and use it in GitHub Desktop.
Save anonymous/626240 to your computer and use it in GitHub Desktop.
http://vanillaforums.org/discussion/13136/attachments-to-fileupload-importer/p1
First run this query to insert your old attachments table data into FileUpload table.
INSERT INTO GDN_Media (Name, Type, Size, StorageMethod, Path, InsertUserID, ForeignID, ForeignTable) SELECT Name, MimeType, Size, 'local', Path, 1, CommentID, 'comment' from LUM_Attachment
After that you need to extract the necessary data from the GDN_Media table in CSV format.
Run an SQL query:
SELECT Path, MediaID FROM `GDN_Media`
When the result comes up, click the "Export" button at the bottom of the page and export as CSV
Save the file onto your server
You should have a list of paths to media ids, like this (I made a copy of my old attachments inside FileUpload):
"FileUpload/2010/07/filename01.jpg";"3626"
"FileUpload/2010/07/filename02.jpg";"3625"
Once that's done, file in the nessecary varibles in the php-file an run it.
ruby paths_to_mediaid_paths.rb <name of csv text file> <root path of source images> <root path of FileUpload folder, including FileUpload>
There's a final parameter to the script, which is the "dispersion factor"
That value is saved in the FileUpload plugin's option "Plugin.FileUpload.DispersionFactor"
<?php
/* Someone should write an GUI for that.
* a form with files:
* csv_path, path to attachments-files, path to fileUpload-folder, checkbox wether it's a testrun or files should be copied, dispersion-factor
*
* What does this script?
* 1) Takes the variables form the form or of the config-section
* 2) Read in the files out of the CSV-file
* 3) copy the files from "attachments" to "fileUpload" folder
*/
/* Config section
* fill in your paths and so on
*/
// if true, only testrun, dont copy files
$testrun = false;
// the path to your CSV-file
$csv_path = "";
// path to your old attachments folder
$from_attachments_folder_path = "";
// path to your new fileUpload folder
$to_fileUpload_folder_path = "";
// dispersion-factor spread for files to many folders - by default 20, INTEGER
$dispersion_factor = NULL ; // integer
/* Checking the input variables
*/
// Check for testrun
if ($testrun == true){
echo "Lame operation, no changes will be made.";
$lame = true;
}
// check for csv_path
// may there be a much shorter way to write that!
// may should check if file exists
if (!isset($csv_path)){
print "No input CSV path given.";
break; // End script here with error message
} else{
$input_path = $csv_path;
}
// check for attachments-folder
// may there be a much shorter way to write that!
// may should check if folder exists
if (!isset($from_attachments_folder_path)){
print "No source images base path given.";
break; // End script here with error message
} else{
$source_images_path = $from_attachments_folder_path;
}
// check for attachments-folder
// may there be a much shorter way to write that!
// may should check if folder exists
if (!isset($to_fileUpload_folder_path)){
print "No dest images base path given.";
break; // End script here with error message
} else{
$dest_images_path = $to_fileUpload_folder_path;
}
// check for dispersion-factor - must be integer
if (!isset($dispersion_factor)){
$dispersion_factor == 20;
print "No dispersion factor given, using default of 20, please specify Plugin.FileUpload.DispersionFactor if different from default.";
//Don't break the script
}
// status message
echo "Reading form". $from_attachments_folder_path;
$path_to_id = array() // new array
// open CSV-file
$handle = fopen ($input_path, "r");
// read file
while (!feof($handle)) {
$line = fgets($handle);
// get infos - example for line: "FileUpload/2010/07/filename01.jpg";"3626"
preg_match("/^\"(.+?)\";\"(\d+?)\"/", $line, $result);
// if line is empty - error message and next line
if (!isset($result) {
echo "No match for line" . $line . ",skipping";
continue;
}
//key is path to file, value is media_id
$path_to_id[result[1]] = result[2];
} // End read file
// Count records -> status message
echo count($path_to_id) . "records read.";
foreach ($path_to_id as $path => $media_id){
$source_path = $source_image_path ."/". $path; //get path to source file
$path_parts = pathinfo($source_path); // get extetion of the file
$source_extention = $path_parts['extension'];
$dispersion_id = $media_id % $dispersion_factor; // get dispersion_id
$dest_dir = $dest_images_path . "/" . $dispersion_id; // get new folder + dispersion
$dest_path = $dest_dir. "/" . $media_id . $source_extention; //get new file path
// status message
echo "Copying " . $source_path ." -> " . $dest_path;
// Copying
try {
if ($lame != true){
mkdir($dest_dir);
copy($source_path, $dest_path);
throw new Exception('Error while copying');
}
catch (Exception $e)
{
echo 'Exceptions chaught: ', $e->getMessage(), "\n";
}
// status message
echo "all done, all fine - have fun with fileUpload";
require 'fileutils'
lame = if ARGV[0] == 'lame'
ARGV.shift
puts "Lame operation, no changes will be made."
true
else
false
end
raise "No input CSV path given." if !ARGV[0]
input_path = ARGV[0]
raise "No source images base path given." if !ARGV[1]
source_images_path = ARGV[1]
raise "No dest images base path given." if !ARGV[2]
dest_images_path = ARGV[2]
dispersion_factor = if !ARGV[3]
puts "No dispersion factor given, using default of 20, please specify Plugin.FileUpload.DispersionFactor if different from default."
20
else
ARGV[3].to_i
end
puts "Reading from #{ARGV[0]}..."
path_to_id = {}
File.new(input_path, 'r').each_line do |line|
result = /^\"(.+?)\";\"(\d+?)\"/.match(line)
if !result
puts "No match for line '#{line.chomp}', skipping."
next
end
path_to_id[result[1]] = result[2].to_i
end
puts "#{path_to_id.length} records read."
path_to_id.each do |path, media_id|
source_path = File.join(source_images_path, path)
source_extension = File.extname(source_path)
dispersion_id = media_id % dispersion_factor
dest_dir = File.join(dest_images_path, dispersion_id.to_s)
dest_path = File.join(dest_dir, "#{media_id}#{source_extension}")
puts "Copying '#{source_path}' -> '#{dest_path}'"
begin
FileUtils.mkdir_p(dest_dir) if !lame
FileUtils.cp(source_path, dest_path) if !lame
rescue Exception=>e
puts " Error while copying: #{e}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment