/**
  * Index files from a remote container
  *
  * @param	string	$folder_id	The folder id to refresh. Files will be fetched from its assigned container
  * @return	array
  *
  **/
 public function synchronize($folder_id)
 {
     $folder = Folder::find($folder_id);
     //list of files should be obtainable via files
     $files = Files::listFiles($folder->location, $folder->remote_container);
     // did the fetch go ok?
     if ($files['status']) {
         $valid_records = array();
         $known = array();
         $known_files = File::findByFolderId($folder_id);
         // now we build an array with the database filenames as the keys so we can compare with the cloud list
         foreach ($known_files as $item) {
             $known[$item->filename] = $item;
         }
         foreach ($files['data'] as $file) {
             // it's a totally new file
             if (!array_key_exists($file['filename'], $known)) {
                 $insert = array('id' => substr(md5(microtime() + $data['filename']), 0, 15), 'folder_id' => $folder_id, 'user_id' => ci()->current_user->id, 'type' => $file['type'], 'name' => $file['filename'], 'filename' => $file['filename'], 'path' => $file['path'], 'description' => '', 'extension' => $file['extension'], 'mimetype' => $file['mimetype'], 'filesize' => $file['filesize'], 'date_added' => $file['date_added']);
                 // we add the id to the list of records that have existing files to match them
                 $valid_records[] = File::create($insert);
             } else {
                 // it's totally not a new file
                 // update with the details we got from the cloud
                 File::where('id', $known[$file['filename']]->id)->update($file);
                 // we add the id to the list of records that have existing files to match them
                 $valid_records[] = $known[$file['filename']]->id;
             }
         }
         // Ok then. Let's clean up the records with no files and get out of here
         File::where('folder_id', $folder_id)->whereNotIn('id', $valid_records)->delete();
         return $this->result(true, trans('files.synchronization_complete'), $folder->name, $files['data']);
     } else {
         return $this->result(null, trans('files.no_records_found'));
     }
 }