<?php
import('Dataface/ImportRecord.php');
class tables_files_Importer_fs extends Dataface_ImportRecord {
function tables_files_Importer_fs($data){
$this->Dataface_ImportRecord(__FILE__, $data);
}
function commit(){
$item = new Dataface_Record('files', array());
$field =& $item->_table->getField('file');
$savepath = $field['savepath'];
$filename = basename($this->getValue('file'));
$parts = explode('.', $filename);
if ( count($parts)>1 ) $ext = '.'.$parts[count($parts)-1];
else $ext='';
$filesavepath = $savepath.'/'.$filename;
$origpath = DATAFACE_SITE_PATH.'/import/'.$filename;
$i = 0;
//echo $filesavepath;exit;
while (file_exists($filesavepath)){
$filename = basename(tempnam($savepath, $this->getValue('file'))).$ext;
$filesavepath = $savepath.'/'.$filename;
if ( $i++ > 10 ) trigger_error("Failed to rename file ".$this->getValue('file'), E_USER_ERROR);
}
//if ( preg_match('/^s3:\/\//', $filesavepath) ){
// $fh = fopen($filesavepath, WRITE_PUBLIC);
// if ( !$fh ) trigger_error("Failed to open file for writing ".$filesavepath, E_USER_ERROR);
// fwrite($fh, file_get_contents($origpath));
// fclose($fh);
// unlink($origpath);
//} else {
if ( !rename($origpath, $filesavepath) ){
trigger_error("Failed to rename file ".$this->getValue('file')." from ".$origpath.' to '.$filesavepath, E_USER_ERROR);
}
//}
$this->setValue('file',$filename);
$item->setValues($this->getValues());
$item->save();
}
}