Component source files Dropbox for MODX Revolution
For one project there was a need to use any cloud storage. The bottom line is that there are several workers, each of which uses a special program, the result of which are separate XML files. Data from these cookies are essential in order to generate reports on the website where users could download the information available to them. The problem is that employees work on their local machines, and how would the Internet do not interact (that is, not using a single server or something). And so I decided to use Dropbox, the type of mood each user his daddy, which will synchronize their files, and on the side of the site the cron will pick up with dropbox and use for reporting. Under the cut will tell you what happened.
Actually, originally I thought it will be much easier, since quite a long time already came across ready component that installs ALL the source files, interacting with Dropbox, but when I installed it, I just got a bunch of fatal errors. The component was designed in 2012 year, and whether used old Dropbox PHP SDK, or do something samobytnoe was, but in any case it has not worked. In the end, it was clear that everything needs to be rewritten. Examples of the special I found online, but dropbox glad pretty good documentation here and here. In the end, I took in the package source object files for the Foundation, poured the latest version of PHP SDK (currently 1.1.4) and finished to a condition when using the admin area of the website that you can manage files in dropbox.
This resulted in the development of the package dropbox-2.0.0-beta. But the rest did not give the idea that I have no other opportunities to from dropbox the data changes. Just as I understand it, I have to deal with tens of thousands of documents in the end, and I can't every time just brute force go through the folders and check of the audit already on the side of the site (by the way, at a time dropbox method Client::getMetadataWithChildren($path) will never return more than 2000 records, although the method provided 25000). Picking the docks, got to method Client::getDelta($cursor = null, $pathPrefix = null). This is just what we need, as passing the well-known cursor you can obtain information about changes since its creation. But here is a small nuisance: how to get the last cursor? The fact is that if a dropbox account has long existed, it in history can accumulate a bunch of changes, and a simple enumeration of cursors can be very long to get up-to-date. Interestingly, there is no such method in the PHP SDK, although in the API for this has a method. Well, okay, if is in the kernel method, not long mobility your client class to know how to obtain this information. Now you can fix for yourself a starting point and to follow the changes without strumming. When I got it, I understood that these data would be convenient to store in the database, and some specialized methods the dropbox client to make a specific entity. Then just show a few examples of what's happened.
the
Of course before to do something, you have to register your Dropbox app (of course, you must be registered in the Dropbox).
the
After installing the package Dropbox in the system, a new type of file sources — Dropbox. Create mediators with this type.
If all OK, then after saving the source files should show the contents of your dropbox (see first picture above).
the
The first thing I did in the updated version of a package is made the ability to get Dropbox client in a separate method mediasource. It can be obtained, for example, like this:
the
This is convenient because you can have as many mediatorov and bother with their settings, each mediators will work with your dropbox-folder.
the
the
the
the
Is needed here two stages:
the
the
In General, if you dig the official documentation is a lot on the basis to do so. In General, there is a desire to develop a MODX component to a file Manager which could share files between different types mediatorov, but so far there's no time/resources.
Download a package of IRepository modx.com
Project at github: github.com/Fi1osof/modx-Dropbox
And finally, small tip about one insidious problem faced during the development of the component.
UPD: IN the process discovered a couple of points:
1. Dropbox PHP SDK requires PHP 5.3 or lower
2. He apparently requires a 64-bit axis. Discussion here.
Article based on information from habrahabr.ru
Actually, originally I thought it will be much easier, since quite a long time already came across ready component that installs ALL the source files, interacting with Dropbox, but when I installed it, I just got a bunch of fatal errors. The component was designed in 2012 year, and whether used old Dropbox PHP SDK, or do something samobytnoe was, but in any case it has not worked. In the end, it was clear that everything needs to be rewritten. Examples of the special I found online, but dropbox glad pretty good documentation here and here. In the end, I took in the package source object files for the Foundation, poured the latest version of PHP SDK (currently 1.1.4) and finished to a condition when using the admin area of the website that you can manage files in dropbox.
This resulted in the development of the package dropbox-2.0.0-beta. But the rest did not give the idea that I have no other opportunities to from dropbox the data changes. Just as I understand it, I have to deal with tens of thousands of documents in the end, and I can't every time just brute force go through the folders and check of the audit already on the side of the site (by the way, at a time dropbox method Client::getMetadataWithChildren($path) will never return more than 2000 records, although the method provided 25000). Picking the docks, got to method Client::getDelta($cursor = null, $pathPrefix = null). This is just what we need, as passing the well-known cursor you can obtain information about changes since its creation. But here is a small nuisance: how to get the last cursor? The fact is that if a dropbox account has long existed, it in history can accumulate a bunch of changes, and a simple enumeration of cursors can be very long to get up-to-date. Interestingly, there is no such method in the PHP SDK, although in the API for this has a method. Well, okay, if is in the kernel method, not long mobility your client class to know how to obtain this information. Now you can fix for yourself a starting point and to follow the changes without strumming. When I got it, I understood that these data would be convenient to store in the database, and some specialized methods the dropbox client to make a specific entity. Then just show a few examples of what's happened.
the
Registration API of the application.
Of course before to do something, you have to register your Dropbox app (of course, you must be registered in the Dropbox).
Detail
To do this, go to the page www.dropbox.com/developers/apps and shake Create app (by the Way, the application name must be unique in the Dropbox (because it's possible to publish a public app, so it is better to specify the domain of the website or something because names like myApp, etc. are busy and dropbox will not save)). Types of applications you will create two Drop-ins app and your Dropbox API app, we will create a second.
Here, immediately take a closer look at step 3.
the Can your app be limited to its own folder?
If you select "My app only needs access to files it creates.", the application will have its own folder where you will access the application. I have this folder Applications/[AppName]. That is, the Applications folder is a system for all applications, and then for each app creates its own folder.
And if you choose "My app needs access to files already on Dropbox.", the application will have access to the root folder and then the list of available files will be limited to only selected types of files. That is, if you choose this option, there is a new step What type of files does your app need access to? and there you can choose either "All file types" (that is, all files) or "Specific file types" and specify what types of files are available.
After we clicked "Create App", the app will be created and we opened the settings page of the app. We in addition find the App key and App secret, but we won't need them, their use will be discussed later. And while we are interested in Access token. Generate it and skopipastil somewhere in the inaccessible place. Again it will not get to see. However, you can generate a new token, and interestingly — will work and prior. I generate three tokens and they all work, I can't see the list of tokens used and cancel are not needed, can just delete the entire application. Katz not secure a bit, but it's more feature than bug.
Here, immediately take a closer look at step 3.
the Can your app be limited to its own folder?
If you select "My app only needs access to files it creates.", the application will have its own folder where you will access the application. I have this folder Applications/[AppName]. That is, the Applications folder is a system for all applications, and then for each app creates its own folder.
And if you choose "My app needs access to files already on Dropbox.", the application will have access to the root folder and then the list of available files will be limited to only selected types of files. That is, if you choose this option, there is a new step What type of files does your app need access to? and there you can choose either "All file types" (that is, all files) or "Specific file types" and specify what types of files are available.
After we clicked "Create App", the app will be created and we opened the settings page of the app. We in addition find the App key and App secret, but we won't need them, their use will be discussed later. And while we are interested in Access token. Generate it and skopipastil somewhere in the inaccessible place. Again it will not get to see. However, you can generate a new token, and interestingly — will work and prior. I generate three tokens and they all work, I can't see the list of tokens used and cancel are not needed, can just delete the entire application. Katz not secure a bit, but it's more feature than bug.
the
source configuration file
After installing the package Dropbox in the system, a new type of file sources — Dropbox. Create mediators with this type.
Detail
When created source files, open it for editing and specify the Access Token of the application.
When created source files, open it for editing and specify the Access Token of the application.
If all OK, then after saving the source files should show the contents of your dropbox (see first picture above).
the
Dropbox-client
The first thing I did in the updated version of a package is made the ability to get Dropbox client in a separate method mediasource. It can be obtained, for example, like this:
the
$source_id = 27; // ID of your source files
$source = $modx- > getObject('modMediaSource', $source_id);
$client = $source->getClient();
This is convenient because you can have as many mediatorov and bother with their settings, each mediators will work with your dropbox-folder.
the
Get information about the contents of directories
the
$source_id = 27; // ID of your source files
$source = $modx- > getObject('modMediaSource', $source_id);
$client = $source->getClient();
$result = $client->getMetadataWithChildren('/');
print_r($result);
Result
Array
(
[hash] => cfcf0b65c94cff03c32390b3c30a5812
[thumb_exists] =>
[bytes] => 0
[path] => /
[is_dir] => 1
[icon] => folder
[root] = > dropbox
[contents] => Array
(
[0] => Array
(
[rev] => 115ce031c0d71
[thumb_exists] => 1
[path] => /315a73ec24 - copied
[is_dir] =>
[client_mtime] => Fri, 05 Dec 2014 23:12:30 +0000
[icon] = > page_white_picture
[bytes] => 258713
[modified] = > Mon, 08 Dec 2014 02:45:31 +0000
[size] => 252.6 KB
[root] = > dropbox
[mime_type] = > image/jpeg
[revision] => 71118
)
[1] => Array
(
[rev] => 102d6031c0d71
[thumb_exists] => 1
[path] => /315a73ec24.jpg
[is_dir] =>
[client_mtime] => Fri, 05 Dec 2014 23:12:30 +0000
[icon] = > page_white_picture
[bytes] => 258713
[modified] = > Sat, 06 Dec 2014 21:21:20 +0000
[size] => 252.6 KB
[root] = > dropbox
[mime_type] = > image/jpeg
[revision] => 66262
)
)
[size] => 0 bytes
)
the
$source_id = 27; // ID of your source files
$source = $modx- > getObject('modMediaSource', $source_id);
$content = $source->getContent('/315a73ec24.jpg');
Or so
$source_id = 27; // ID of your source files
$entry = $modx- > newObject('DropboxEntry', array(
"source_id" = > $source_id,
"path" => '/315a73ec24.jpg',
));
$content = $entry->getContent(); // Or $entry->get('content');
Or so
$source_id = 27; // ID of your source files
$source = $modx- > getObject('modMediaSource', $source_id);
$entry = $modx- > newObject('DropboxEntry', array(
"path" => '/315a73ec24.jpg',
));
$entry->Source = $source;
$content = $entry->getContent(); // Or $entry->get('content');
Or so (if the record is already in database)
$entry = $modx- > getObject('DropboxEntry', array(
"path" => '/315a73ec24.jpg',
));
$content = $entry->getContent(); // Or $entry->get('content');
the
the Tracked changes in the sections
Is needed here two stages:
1. Get the current cursor for the section and save it to the database
in General, do not necessarily save him, but it's more convenient, will not disappear.
the
the
$source_id = 27; // ID of your source files
$cursor = $modx- > newObject('DropboxCursor', array(
"source_id" = > $source_id,
"path" => '/',
));
if($result = $cursor->getLast()){
$cursor->fromArray($result);
$cursor->save();
}
2. The requested changes.
$cursor= $modx- > getObject('DropboxCursor', array(
"path" => '/',
));
$result = $entry->getDelta();
print_r($result);
Response
Array
(
[has_more] =>
[cursor] => AAE3sefSDffe3SiOcfkaMd7vyzjQvjxngki1gkjglvbqplsnuhf9tx5okb7epngufiziudwjyg16swaxjrcnt-XjT-jBD_8fDW89enF7OO3AHBjsZz9vHTBJenmecdlugo0f-EqRJYigzELD0rzD3hFNn-YGfDQaxUOs0bHd2F5qND3J6HX-HwOqOLWQfcbuMZMNHN1_UGN2VODIbaPzbbml0rap_b6ibv03jqxtighztxwjp-mSw8drigK3yJtvqyR3XdzHP5uVsyrD8gkygk1j9dotldzfra-woImCg1Iwc9XA7l1XsZGCRvDYpXjg
[entries] => Array
(
)
[reset] =>
)
If anything changes, you'll see them in the mountains entries.the
Audit files
the
$entry = $modx- > getObject('DropboxEntry', array(
"path" => '/315a73ec24.jpg',
));
$result = $entry- > getRevisions();
And you can restore the desired version of the file.
$entry = $modx- > getObject('DropboxEntry', array(
"path" => '/315a73ec24.jpg',
));
$result = $entry->restoreFile('102d6031c0d71');
In General, if you dig the official documentation is a lot on the basis to do so. In General, there is a desire to develop a MODX component to a file Manager which could share files between different types mediatorov, but so far there's no time/resources.
Download a package of IRepository modx.com
Project at github: github.com/Fi1osof/modx-Dropbox
And finally, small tip about one insidious problem faced during the development of the component.
UPD: IN the process discovered a couple of points:
1. Dropbox PHP SDK requires PHP 5.3 or lower
2. He apparently requires a 64-bit axis. Discussion here.
Комментарии
Отправить комментарий