Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Download movies and/or episodes works now
  • Loading branch information
tamplan committed Jan 29, 2012
1 parent 1a8c1c5 commit 7212463
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 19 deletions.
64 changes: 64 additions & 0 deletions application/controllers/files.php
@@ -0,0 +1,64 @@
<?php

class Files extends CI_Controller
{

public function __construct()
{
parent::__construct();

// Chargement des modèles de la base de données 'xbmc_video'
$this->load->model('video/actors_model');
$this->load->model('video/countries_model');
$this->load->model('video/episodes_model');
$this->load->model('video/video_files_model');
$this->load->model('video/genres_model');
$this->load->model('video/movies_model');
$this->load->model('video/video_paths_model');
$this->load->model('xbmc/sources_model');
$this->load->model('video/sets_model');
$this->load->model('video/studios_model');
$this->load->model('video/tvshows_model');
}

/**
* Demande à télécharger un film ou un épisode
*/
public function ajax_download()
{
// L'utilisateur connecté peut télécharger des vidéos ?
if ($this->session->userdata('can_download_video'))
{
// Champ concerné par la modification
list($type, $id) = explode('_', $this->uri->segments[3]);

switch($type)
{
case 'movie':
$files = $this->movies_model->get($id, FALSE);
$filename = $files[0]->filename;
$path = str_replace($files[0]->source->client_path, $files[0]->source->server_path, $files[0]->path);
break;

case 'episode':
$files = $this->episodes_model->get($id, FALSE);
$filename = $files[0]->filename;
$path = str_replace($files[0]->source->client_path, $files[0]->source->server_path, $files[0]->path);
break;
}

// Chargement de la focntion 'force_download'
$this->load->helper('MY_download');

// Force le téléchargement du fichier
force_download($filename, $path.$filename);
}

// Sortie avec téléchargement de fichier ou pas
die();
}

}

/* End of file files.php */
/* Location: ./system/application/controllers/files.php */
120 changes: 120 additions & 0 deletions application/helpers/my_download_helper.php
@@ -0,0 +1,120 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Force Download
*
* Generates headers that force a download to happen
*
* @access public
* @param string filename
* @param mixed the data to be downloaded
* @return void
*/
if ( ! function_exists('force_download'))
{
function force_download($filename = '', $file = '')
{
if ($filename == '' OR $file == '')
{
return FALSE;
}

// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
if (FALSE === strpos($filename, '.'))
{
return FALSE;
}

// Grab the file extension
$x = explode('.', $filename);
$extension = end($x);

// Load the mime types
@include(APPPATH.'config/mimes'.EXT);

// Set a default mime if we can't find it
if ( ! isset($mimes[$extension]))
{
$mime = 'application/octet-stream';
}
else
{
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}

// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($file));
}
else
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($file));
}

readfile_chunked($file);
die;
}
}

/**
* readfile_chunked
*
* Reads file in chunks so big downloads are possible without changing PHP.INI
*
* @access public
* @param string file
* @param boolean return bytes of file
* @return void
*/
if ( ! function_exists('readfile_chunked'))
{
function readfile_chunked($file, $retbytes=TRUE)
{
$chunksize = 1 * (1024 * 1024);
$buffer = '';
$cnt =0;

$handle = fopen($file, 'r');
if ($handle === FALSE)
{
return FALSE;
}

while (!feof($handle))
{
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();

if ($retbytes)
{
$cnt += strlen($buffer);
}
}

$status = fclose($handle);

if ($retbytes AND $status)
{
return $cnt;
}

return $status;
}
}

/* End of file MY_download_helper.php */
/* Location: ./application/helpers/MY_download_helper.php */
39 changes: 23 additions & 16 deletions application/models/video/episodes_model.php
Expand Up @@ -84,11 +84,14 @@ function get_ids($tvshow_id, $season_number, $episode_number = 0)
* Retourne un épisode d'une série tv dont on précise l'identifiant
* On précise l'identifiant de l'épisode ou un tableau d'identifiant
*
* Permet d'avoir plus de données si $for_view vaut TRUE
*
* @access public
* @param integer
* @return object
* @param integer or array
* @param boolean
* @return array
*/
function get($episode_id)
function get($idEpisode, $for_view = TRUE)
{
// Liste et noms des champs à récupérer
$fields[] = 'idEpisode';
Expand All @@ -110,22 +113,22 @@ function get($episode_id)
$fields = implode(', ', $fields);

// Est-ce un tableau d'identifiants ?
if (is_array($episode_id))
if (is_array($idEpisode))
{
$results = $this->{$this->_db_group_name}->select($fields)
->from('episodeview')
->order_by('idShow', 'ASC')
->order_by('season_number', 'ASC')
->order_by('episode_number', 'ASC')
->where_in('idEpisode', $episode_id)
->where_in('idEpisode', $idEpisode)
->get()
->result();
}
else
{
$results = $this->{$this->_db_group_name}->select($fields)
->from('episodeview')
->where('idEpisode', $episode_id)
->where('idEpisode', $idEpisode)
->get()
->result();
}
Expand Down Expand Up @@ -178,17 +181,21 @@ function get($episode_id)
else
$episode->runtime = $this->lang->line('media_no_runtime');

$episode->poster_url = $result->poster_url;
$episode->tvshow_id = $result->idShow;
$episode->tvshow_name = $result->strTitle;
$episode->season_number = $result->season_number;
$episode->episode_number = $result->episode_number;
// Consulation de la page détaillée d'un épisode ?
if ($for_view)
{
$episode->poster_url = $result->poster_url;
$episode->tvshow_id = $result->idShow;
$episode->tvshow_name = $result->strTitle;
$episode->season_number = $result->season_number;
$episode->episode_number = $result->episode_number;

$episode->poster = $this->xbmc_lib->get_episode_poster($episode);
$episode->poster = $this->xbmc_lib->get_episode_poster($episode);

$episode->writers = $this->_CI->actors_model->get_writers_for_episode($episode->id);
$episode->directors = $this->_CI->actors_model->get_directors_for_episode($episode->id);
$episode->actors = $this->_CI->actors_model->get_actors_for_episode($episode->id);
$episode->writers = $this->_CI->actors_model->get_writers_for_episode($episode->id);
$episode->directors = $this->_CI->actors_model->get_directors_for_episode($episode->id);
$episode->actors = $this->_CI->actors_model->get_actors_for_episode($episode->id);
}

// Ajoût de l'épisode dans un tableau pour retour
$episodes[] = $episode;
Expand All @@ -199,7 +206,7 @@ function get($episode_id)
$episodes = array();
}

// On retourne le(s) film(s) trouvé(s) ou NULL
// On retourne le ou les épisode(s) trouvé(s) ou un tableau vide
return $episodes;
}

Expand Down
2 changes: 1 addition & 1 deletion application/models/video/movies_model.php
Expand Up @@ -804,7 +804,7 @@ function get_all($limit = NULL, $offset = NULL)
}

// On récupère enfin les films
$movies = $this->movies_model->get($idsMovie);
$movies = $this->get($idsMovie);
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions application/views/content/video/episodes/view.php
@@ -1,6 +1,7 @@
<script type="text/javascript">
<!--
tvshow_id = <?php echo $episode->tvshow_id; ?>;
media_id = 'episode_<?php echo $episode->id; ?>';
//-->
</script>
<script src="<?php echo base_url(); ?>assets/scripts/tabs.js" language="javascript" type="text/javascript"></script>
Expand All @@ -9,6 +10,10 @@
<script src="<?php echo base_url(); ?>assets/scripts/episodes_infos.js" language="javascript" type="text/javascript"></script>
<?php endif; ?>

<?php if ($this->session->userdata('can_download_video')): ?>
<script src="<?php echo base_url(); ?>assets/scripts/download.js" language="javascript" type="text/javascript"></script>
<?php endif; ?>

<div id="main">
<div class="block">
<div class="secondary-navigation">
Expand Down
5 changes: 5 additions & 0 deletions application/views/content/video/movies/view.php
Expand Up @@ -9,6 +9,7 @@
<script type="text/javascript">
<!--
movie_id = <?php echo $movie->id; ?>;
media_id = 'movie_<?php echo $movie->id; ?>';
poster_filename = '<?php echo $movie->poster->filename; ?>';
backdrop_filename = '<?php echo $movie->backdrop->filename; ?>';
//-->
Expand All @@ -23,6 +24,10 @@
<script src="<?php echo base_url(); ?>assets/scripts/images.js" language="javascript" type="text/javascript"></script>
<?php endif; ?>

<?php if ($this->session->userdata('can_download_video')): ?>
<script src="<?php echo base_url(); ?>assets/scripts/download.js" language="javascript" type="text/javascript"></script>
<?php endif; ?>

<?php
if ($this->session->userdata('can_change_images'))
{
Expand Down
5 changes: 3 additions & 2 deletions application/views/includes/buttons/download.php
@@ -1,3 +1,4 @@
<button id="download-button" class="button" type="button">
<img src="<?php echo base_url(); ?>assets/gui/download.png" /> <?php echo $this->lang->line('btn_download'); ?>
</button>
<img src="<?php echo base_url(); ?>assets/gui/download.png" /> <?php echo $this->lang->line('btn_download'); ?>
</button>
<iframe id="download_frame" name="uploadFrame" src="#" style="display:none;"></iframe>
9 changes: 9 additions & 0 deletions assets/scripts/download.js
@@ -0,0 +1,9 @@
jQuery(document).ready(function() {

// Attache le téléchargement d'un film ou d'un épisode
$("#download-button").click(function(){
$("#download_frame").attr("src",site_url+"files/download/"+media_id);
return false;
});

});

0 comments on commit 7212463

Please sign in to comment.