aboutsummaryrefslogtreecommitdiff
path: root/types/uploadfield/uploadfield_file.inc
blob: cff6c7fedf00dd2b2c3208f21a68b1fc4cffea65 (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
<?php
// $Id$

/**
 * @file
 * hook_file and uploadfield file functions.
 */

/**
 * Implementation of hook_file_insert().
 */
function uploadfield_file_insert($file) {
// Currently empty. Thumbnails are now generated on preview.
}

/**
 * Implementation of hook_file_delete().
 *
 * Delete the admin thumbnail when the original is deleted.
 */
function uploadfield_file_delete($file) {
// delete thumb from files
//  file_set_status($file->data['video_thumb_fid'], FILE_STATUS_TEMPORARY);
  db_query('DELETE FROM {files} WHERE fid = %d', $file->data['video_thumb_fid']);

  // delete thums directory
  $thumbs_path = variable_get('video_thumb_path', 'video_thumbs');
  //files will save in files/video_thumbs/#fileId folder
  $tmp = file_directory_path(). '/' . $thumbs_path . '/' . $file->fid . '/';
  //  rmdir($tmp);
  _file_delete_tree($tmp);
//  if(@rmdir($tmp)) {
//  }
}


// delete dir and its content
function _file_delete_tree($current_dir) {
  if($dir = @opendir($current_dir)) {
    while (($f = readdir($dir)) !== false) {
      if($f > '0' and filetype($current_dir.$f) == "file") {
        file_delete($current_dir.$f);
      } elseif($f > '0' and filetype($current_dir.$f) == "dir") {
        _file_delete_tree($current_dir.$f."\\");
      }
    }
    closedir($dir);
    rmdir($current_dir);
  }
}

/**
 * Simple utility function to check if a file is an image.
 */
function uploadfield_file_is_image($file) {
  return TRUE;
  //TODO : add file validations
  $file = (object)$file;
  return in_array($file->filemime, array('image/jpg', 'image/pjpeg', 'image/jpeg', 'image/png', 'image/gif'));
}

/**
 * Given a file, return the path the image thumbnail used while editing.
 */
function uploadfield_file_admin_thumb_path($file, $create_thumb = TRUE) {
  $file = (object)$file;
  $short_path = preg_replace('/^' . preg_quote(file_directory_path(), '/') . '/', '', $file->filepath);
  $filepath = file_directory_path() . '/video_thumb' . $short_path;

  if ($create_thumb) {
    uploadfield_create_admin_thumb($file->filepath, $filepath);
  }

  return $filepath;
}

/**
 * Create a thumbnail to be shown while editing an image.
 */
function uploadfield_create_admin_thumb($source, $destination) {
  if (!is_file($source)) {
    return FALSE;
  }

  $info = image_get_info($source);
  $size = explode('x', variable_get('uploadfield_thumb_size', '100x100'));

  // Check if the destination image needs to be regenerated to match a new size.
  if (is_file($destination)) {
    $thumb_info = image_get_info($destination);
    if ($thumb_info['width'] != $size[0] && $thumb_info['height'] != $size[1] && ($info['width'] > $size[0] || $info['height'] > $size[1])) {
      unlink($destination);
    }
    else {
      return;
    }
  }

  // Ensure the destination directory exists and is writable.
  $directories = explode('/', $destination);
  array_pop($directories); // Remove the file itself.
  // Get the file system directory.
  $file_system = file_directory_path();
  foreach ($directories as $directory) {
    $full_path = isset($full_path) ? $full_path . '/' . $directory : $directory;
    // Don't check directories outside the file system path.
    if (strpos($full_path, $file_system) === 0) {
      field_file_check_directory($full_path, FILE_CREATE_DIRECTORY);
    }
  }

  // Create the thumbnail.
  if ($info['width'] <= $size[0] && $info['height'] <= $size[1]) {
    file_copy($source, $destination);
  }
  elseif (image_get_toolkit() && @image_scale($source, $destination, $size[0], $size[1])) {
  // Set permissions. This is done for us when using file_copy().
    @chmod($destination, 0664);
  }
  else {
    drupal_set_message(t('An image thumbnail was not able to be created.'), 'error');
  }
}