aboutsummaryrefslogtreecommitdiff
path: root/modules/video_s3/video_s3.module
blob: 5762fef52952c78c5f5c82ca00e3059b05f3e849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

/**
 * @file
 * Provides wrapper functions for the s3 amazon webservices.
 */
/*
 * Implementation of hook_perm().
 */
function video_s3_perm() {
  return array('administer amazon s3');
}

/*
 * Implmentation of hook_help().
 */

function video_s3_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/video/amazon_s3':
      $output = t('Use Amazon Simple Storage Service (Amazon S3) to store your video files.  This free\'s up bandwidth from your site, providing a faster experience for your users.  Simply enable this module and enter your authentication details and your done!');
      return $output;
  }
}

/*
 * Implementation of hook_menu().
 */

function video_s3_menu() {
  $items = array();
  $items['admin/settings/video/amazon_s3/bucket/%/delete'] = array(
    'title' => 'Delete Bucket',
    'page callback' => 'video_s3_bucket_delete',
    'page arguments' => array(5),
    'access arguments' => array('administer amazon s3'),
    'file' => 'video_s3.admin.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/*
 * Implementation of hook_cron().
 */

function video_s3_cron() {
  $filesystem = variable_get('video_filesystem', 'drupal');
  if ($filesystem == 'video_s3') {
    module_load_include('inc', 'video_s3', '/includes/amazon_s3');
    $s3 = new video_amazon_s3;
    $s3->connect();
    // Lets run our queue.
    $s3->queue();
  }
}

/**
 * Implementation of hook_video_delete.
 * we can use hook_file_delete()
 */
function video_s3_video_delete($file) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  $s3->connect();
  // Lets run our queue.
  $s3->delete($file->fid);
}

/*
 * Implementation of hook_video_update.
 * Submit hanlder to update our s3 table to include the node id.
 */

function video_s3_video_update($form, &$form_state) {
  //lets update our video rending table to include the node id created
  if (isset($form_state['nid']) && isset($form_state['values']['video_id']) && is_array($form_state['values']['video_id'])) {
    foreach ($form_state['values']['video_id'] as $fid) {
      //lets update our table to include the nid
      db_query("UPDATE {video_s3} SET nid=%d WHERE fid=%d", $form_state['nid'], $fid);
    }
  }
}

/**
 * Implementing hook_video_insert
 * @param <type> $element
 * @param <type> $form_state
 */
function video_s3_video_insert(&$element, &$form_state) {
  $file = $element['#value'];
  //we need to check if this fid has already been added to the database AND that there is in fact a fid
  if (is_array($file) && isset($file['fid']) && !empty($file['fid'])) {
    module_load_include('inc', 'video_s3', '/includes/amazon_s3');
    $s3 = new video_amazon_s3;
    $s3->connect();
    // Lets verify that we haven't added this video already.  Multiple validation fails will cause this to be ran more than once
    if (!$video = $s3->verify($file['fid'])) {
      // Video has not been added to the queue yet so lets add it.
      $s3->insert($file['fid']);
      drupal_set_message(t('Video submission queued for transfer to your Amazon S3 server. Will be there shortly.'));
    }
  }
}

/**
 * Implementing hook_video_load
 * @param <type> $element
 * @param <type> $form_state
 */
function video_s3_video_load(&$video) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  if ($amazon = $s3->get($video->fid)) {
    // Fix our filepath
    $video->filepath = $amazon->filepath;
//    $video->url = $amazon->filepath;
    if (variable_get('amazon_s3_private', FALSE))
      $video->files->{$video->player}->url = video_s3_get_authenticated_url($amazon->filename);
    else
      $video->files->{$video->player}->url = $amazon->filepath;

    $video->extension = pathinfo($amazon->filepath, PATHINFO_EXTENSION);
  }
}

function video_s3_get_object_info($object) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  $s3->connect();
  return $s3->get_object_info($object);
}

function video_s3_get_authenticated_url($object) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  $s3->connect();
  return $s3->get_authenticated_url($object);
}

function video_s3_get_object($object, $save_to = false) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  $s3->connect();
  return $s3->get_object($object, $save_to);
}

/*
 * Deletes a bucket from your Amazon S3 server.
 */

function video_s3_bucket_delete($bucket) {
  module_load_include('inc', 'video_s3', '/includes/amazon_s3');
  $s3 = new video_amazon_s3;
  $s3->connect();
  $buckets = $s3->s3->listBuckets();
  if (is_array($buckets) && in_array($bucket, $buckets)) {
    if ($s3->s3->deleteBucket($bucket)) {
      drupal_set_message(t('Successfully deleted the bucket %bucket', array('%bucket' => $bucket)));
    } else {
      drupal_set_message(t('Could not delete the bucket %bucket', array('%bucket' => $bucket)), 'error');
    }
  } else {
    drupal_set_message(t('The bucket %bucket does not exist for deletion.', array('%bucket' => $bucket)), 'error');
  }
  drupal_goto('admin/settings/video/amazon_s3');
}