summaryrefslogtreecommitdiff
path: root/lib/api/folder_move.php
blob: 22eced02fdb6fbb1293a67ee62e1ccf98d7e7dee (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
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab File API                                  |
 |                                                                          |
 | Copyright (C) 2012-2014, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

class file_api_folder_move extends file_api_common
{
    /**
     * Request handler
     */
    public function handle()
    {
        parent::handle();

        if (!isset($this->args['folder']) || $this->args['folder'] === '') {
            throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
        }

        if (!isset($this->args['new']) || $this->args['new'] === '') {
            throw new Exception("Missing destination folder name", file_api_core::ERROR_CODE);
        }

        if ($this->args['new'] === $this->args['folder']) {
            return;
        }

        list($src_driver, $src_path) = $this->api->get_driver($this->args['folder']);
        list($dst_driver, $dst_path) = $this->api->get_driver($this->args['new']);

        // source folder is a mount point (driver title)...
        if ($src_driver->title() === $this->args['folder']) {
            // ... rename
            if (strpos($this->args['new'], file_storage::SEPARATOR) === false) {
                // @TODO
            }

            throw new Exception("Unsupported operation", file_api_core::ERROR_CODE);
        }

        // cross-driver move
        if ($src_driver != $dst_driver) {
            // destination folder is an existing mount point
            if (!strlen($dst_path)) {
                throw new Exception("Destination folder already exists", file_api_core::ERROR_CODE);
            }

            return $this->folder_move_to_other_driver($src_driver, $src_path, $dst_driver, $dst_path);
        }

        return $src_driver->folder_move($src_path, $dst_path);
    }

    /**
     * Move folder between two external storage locations
     */
    protected function folder_move_to_other_driver($src_driver, $src_path, $dst_driver, $dst_path)
    {
        $src_folders = $src_driver->folder_list();
        $dst_folders = $dst_driver->folder_list();

        // first check if destination folder not exists
        if (in_array($dst_path, $dst_folders)) {
            throw new Exception("Destination folder already exists", file_api_core::ERROR_CODE);
        }

        // now recursively create/delete folders and copy their content
        $this->move_folder_with_content($src_driver, $src_path, $dst_driver, $dst_path, $src_folders);

        // now we can delete the folder
        $src_driver->folder_delete($src_path);
    }

    /**
     * Recursively moves folder and it's content to another location
     */
    protected function move_folder_with_content($src_driver, $src_path, $dst_driver, $dst_path, $src_folders)
    {
        // create folder
        $dst_driver->folder_create($dst_path);

        foreach ($src_driver->file_list($src_path) as $filename => $file) {
            $this->file_copy($src_driver, $dst_driver, $filename, $dst_path . file_storage::SEPARATOR . $file['name']);
        }

        // sub-folders...
        foreach ($src_folders as $folder) {
            if (strpos($folder, $src_path . file_storage::SEPARATOR) === 0
                && strpos($folder, file_storage::SEPARATOR, strlen($src_path) + 2) === false
            ) {
                $destination = $dst_path . file_storage::SEPARATOR . substr($folder, strlen($src_path) + 1);
                $this->move_folder_with_content($src_driver, $folder, $dst_driver, $destination, $src_folders);
            }
        }
    }

    /**
     * File move between storage backends
     */
    protected function file_copy($src_driver, $dst_driver, $src_path, $dst_path)
    {
        // unable to put file on mount point
        if (strpos($dst_path, file_storage::SEPARATOR) === false) {
            throw new Exception("Unable to move file into specified location", file_api_core::ERROR_CODE);
        }

        // get the file from source location
        $fp = fopen('php://temp', 'w+');

        if (!$fp) {
            throw new Exception("Internal server error", file_api_core::ERROR_CODE);
        }

        $src_driver->file_get($src_path, null, $fp);

        rewind($fp);

        $chunk = stream_get_contents($fp, 102400);
        $type  = rcube_mime::file_content_type($chunk, $dst_path, 'application/octet-stream', true);

        rewind($fp);

        // upload the file to new location
        $dst_driver->file_create($dst_path, array('content' => $fp, 'type' => $type));

        fclose($fp);
    }
}