/****************************************************************************** Move a SageTV Media File Author: Derek Battams Last Modified: 14 Nov 2010 Use this groovy script to move a SageTV media file object from one location to another. The media file to be moved is defined by the SJQ4_ID environment variable; the SJQ4_TYPE variable must also be "MediaFile". The destination is given as the first command line argument to this script. The destination is a directory. This script will trigger a rescan of your media after a successful move; the rescan is necessary in order for SageTV to recognize the media file in its new location. ******************************************************************************/ boolean testMode = false; /******************* DO NOT EDIT BELOW THIS LINE ************************/ // But if you do then send me your bug fix patches! ;) import org.apache.commons.io.FileUtils; import static groovy.io.FileType.*; import com.google.code.sagetvaddons.sjq.network.ServerClient; String type = SJQ4_METADATA.get("SJQ4_TYPE"); String id = SJQ4_METADATA.get("SJQ4_ID"); File dest = new File(SJQ4_ARGS[0]); if(!dest.isDirectory() && !dest.mkdirs()) { println("Destination directory is invalid! [" + dest.getAbsolutePath() + "]"); return 1; } if(!"MediaFile".equals(type) || id == null || !id.matches("\\d+")) { println("No media file attributes provided!"); return 1; } Object mediaFile = MediaFileAPI.GetMediaFileForID(id.toInteger()); MediaFileAPI.SetMediaFileMetadata(mediaFile, "SJQ4_ARCHIVED", null); if(mediaFile == null) { println("No media file for id " + id); return 1; } if(MediaFileAPI.GetNumberOfSegments(mediaFile) == 0) { println("Zero file segments for media file; nothing to move!"); return 1; } println("Moving " + Arrays.toString(MediaFileAPI.GetSegmentFiles(mediaFile)) + " to destination: " + dest.getAbsolutePath()); // Copy all segments then delete the originals if more than one segment otherwise just do a filesystem move op if(MediaFileAPI.GetNumberOfSegments(mediaFile) == 1) { try { if(!testMode) FileUtils.moveFileToDirectory(MediaFileAPI.GetFileForSegment(mediaFile, 0), dest, false); else println("Would move: " + MediaFileAPI.GetFileForSegment(mediaFile, 0)); } catch(IOException e) { println("Failed to move file to destination!"); e.printStackTrace(); return 1; } } else { def copied = []; // Copy the files to their new home for(File segment : MediaFileAPI.GetSegmentFiles(mediaFile)) { try { if(!testMode) { FileUtils.copyFileToDirectory(segment, dest, true); copied.add(new File(dest, segment.getName())); println("\tCopy of '" + segment.getAbsolutePath() + "' completed..."); } else { println("\tWould move: " + segment.getAbsolutePath()); } } catch(IOException e) { println("Error copying segment '" + segment.getAbsolutePath() + "' to destination!"); e.printStackTrace(); copied.each { if(!it.delete()) println("Failed to delete '" + it.getAbsolutePath() + "'"); } return 1; } } } println("Moving artifacts..."); mvArtifacts(mediaFile, dest, testMode); if(!testMode) { MediaFileAPI.DeleteFile(MediaFileAPI.GetMediaFileForID(id.toInteger())); ServerClient sc = new ServerClient(); sc.scheduleMediaScan(); sc.close(); println("Core media scan triggered..."); } return 0; def mvArtifacts(Object mediaFile, File dest, boolean testMode) { for(File segment : MediaFileAPI.GetSegmentFiles(mediaFile)) { String prefix = segment.getName().substring(0, segment.getName().lastIndexOf('.')); File baseDir = new File(segment.getParent()); baseDir.eachFileMatch FILES, {!new File(baseDir, it.toString()).equals(segment) && it.toString().startsWith(prefix)}, { File artifact = new File(baseDir, it.getName()); if(!testMode) { FileUtils.moveFileToDirectory artifact, dest, false; } else println("Would move artifact: " + artifact.getAbsolutePath()); } } }