File-Dropbox-0.7000755000765000024 012756424432 13035 5ustar00n4kzstaff000000000000File-Dropbox-0.7/Build.PL000444000765000024 66412756424432 14454 0ustar00n4kzstaff000000000000use 5.010; use strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'File::Dropbox', license => 'perl', create_license => 1, create_readme => 1, configure_requires => { 'Module::Build' => '0.40', }, requires => { 'Furl' => '2.16', 'JSON' => '2.0', 'Net::DNS::Lite' => '0.1', 'IO::Socket::SSL' => '1.4', }, ); $builder->create_build_script(); File-Dropbox-0.7/Changes000444000765000024 56412756424432 14452 0ustar00n4kzstaff0000000000000.7 2016-08-22 - Removed experimental code 0.6 2014-02-03 - File operations - Minor fixes 0.5 2013-08-14 - Quota error handling fixed - OAuth 2.0 support added 0.4 2013-08-04 - Net::Curl replaced with Furl - Perl 5.10 support added 0.3 2013-08-03 - WWW::Curl replaced with Net::Curl - Fixed API error handling logic 0.2 2013-05-25 - First release File-Dropbox-0.7/LICENSE000444000765000024 4375712756424432 14237 0ustar00n4kzstaff000000000000This software is copyright (c) 2016 by Alexander Nazarov . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2016 by Alexander Nazarov . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2016 by Alexander Nazarov . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End File-Dropbox-0.7/MANIFEST000444000765000024 42212756424432 14301 0ustar00n4kzstaff000000000000Build.PL Changes lib/File/Dropbox.pm LICENSE MANIFEST META.json META.yml README t/01-basic.t t/02-open.t t/03-write.t t/04-read.t t/05-meta.t t/06-putfile.t t/07-contents.t t/08-timeout.t t/09-movefile.t t/10-copyfile.t t/11-createfolder.t t/99-clean.t t/lib/Test/Common.pm File-Dropbox-0.7/META.json000444000765000024 202112756424432 14606 0ustar00n4kzstaff000000000000{ "abstract" : "Convenient and fast Dropbox API abstraction", "author" : [ "Alexander Nazarov " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4216", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "File-Dropbox", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.40" } }, "runtime" : { "requires" : { "Furl" : "2.16", "IO::Socket::SSL" : "1.4", "JSON" : "2.0", "Net::DNS::Lite" : "0.1" } } }, "provides" : { "File::Dropbox" : { "file" : "lib/File/Dropbox.pm", "version" : "0.7" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.7", "x_serialization_backend" : "JSON::PP version 2.27300" } File-Dropbox-0.7/META.yml000444000765000024 126012756424432 14442 0ustar00n4kzstaff000000000000--- abstract: 'Convenient and fast Dropbox API abstraction' author: - 'Alexander Nazarov ' build_requires: {} configure_requires: Module::Build: '0.40' dynamic_config: 1 generated_by: 'Module::Build version 0.4216, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: File-Dropbox provides: File::Dropbox: file: lib/File/Dropbox.pm version: '0.7' requires: Furl: '2.16' IO::Socket::SSL: '1.4' JSON: '2.0' Net::DNS::Lite: '0.1' resources: license: http://dev.perl.org/licenses/ version: '0.7' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' File-Dropbox-0.7/README000444000765000024 1522312756424432 14075 0ustar00n4kzstaff000000000000NAME File::Dropbox - Convenient and fast Dropbox API abstraction SYNOPSIS use File::Dropbox; use Fcntl; # Application credentials my %app = ( oauth2 => 1, access_token => $access_token, ); my $dropbox = File::Dropbox->new(%app); # Open file for writing open $dropbox, '>', 'example' or die $!; while (<>) { # Upload data using 4MB chunks print $dropbox $_; } # Commit upload (optional, close will be called on reopen) close $dropbox or die $!; # Open for reading open $dropbox, '<', 'example' or die $!; # Download and print to STDOUT # Buffered, default buffer size is 4MB print while <$dropbox>; # Reset file position seek $dropbox, 0, Fcntl::SEEK_SET; # Get first character (unbuffered) say getc $dropbox; close $dropbox; DESCRIPTION "File::Dropbox" provides high-level Dropbox API abstraction based on Tie::Handle. Code required to get "access_token" and "access_secret" for signed OAuth 1.0 requests or "access_token" for OAuth 2.0 requests is not included in this module. To get "app_key" and "app_secret" you need to register your application with Dropbox. At this moment Dropbox API is not fully supported, "File::Dropbox" covers file read/write and directory listing methods. If you need full API support take look at WebService::Dropbox. "File::Dropbox" main purpose is not 100% API coverage, but simple and high-performance file operations. Due to API limitations and design you can not do read and write operations on one file at the same time. Therefore handle can be in read-only or write-only state, depending on last call to open. Supported functions for read-only state are: open, close, seek, tell, readline, read, sysread, getc, eof. For write-only state: open, close, syswrite, print, printf, say. All API requests are done using Furl module. For more accurate timeouts Net::DNS::Lite is used, as described in Furl::HTTP. Furl settings can be overriden using "furlopts". METHODS new my $dropbox = File::Dropbox->new( access_secret => $access_secret, access_token => $access_token, app_secret => $app_secret, app_key => $app_key, chunk => 8 * 1024 * 1024, root => 'dropbox', furlopts => { timeout => 20 } ); my $dropbox = File::Dropbox->new( access_token => $access_token, oauth2 => 1 ); Constructor, takes key-value pairs list access_secret OAuth 1.0 access secret access_token OAuth 1.0 access token or OAuth 2.0 access token app_secret OAuth 1.0 app secret app_key OAuth 1.0 app key oauth2 OAuth 2.0 switch, defaults to false. chunk Upload chunk size in bytes. Also buffer size for "readline". Optional. Defaults to 4MB. root Access type, "sandbox" for app-folder only access and "dropbox" for full access. furlopts Parameter hash, passed to Furl constructor directly. Default options timeout => 10, inet_aton => \&Net::DNS::Lite::inet_aton, ssl_opts => { SSL_verify_mode => SSL_VERIFY_PEER(), } FUNCTIONS All functions are not exported by default but can be exported on demand. use File::Dropbox qw{ contents metadata putfile }; First argument for all functions should be GLOB reference, returned by "new". contents Arguments: $dropbox [, $path] Function returns list of hashrefs representing directory content. Hash fields described in Dropbox API docs . $path defaults to "/". If there is unfinished chunked upload on handle, it will be commited. foreach my $file (contents($dropbox, '/data')) { next if $file->{'is_dir'}; say $file->{'path'}, ' - ', $file->{'bytes'}; } metadata Arguments: $dropbox Function returns stored metadata for read-only handle, closed write handle or after call to "contents" or "putfile". open $dropbox, '<', '/data/2013.dat' or die $!; my $meta = metadata($dropbox); if ($meta->{'bytes'} > 1024) { # Do something } putfile Arguments: $dropbox, $path, $data Function is useful for uploading small files (up to 150MB possible) in one request (at least two API requests required for chunked upload, used in open-write-close sequence). If there is unfinished chunked upload on handle, it will be commited. local $/; open my $data, '<', '2012.dat' or die $!; putfile($dropbox, '/data/2012.dat', <$data>) or die $!; say 'Uploaded ', metadata($dropbox)->{'bytes'}, ' bytes'; close $data; copyfile Arguments: $dropbox, $source, $target Function copies file or directory from one location to another. Metadata for copy can be accessed using "metadata" function. copyfile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!; say 'Created backup with revision ', metadata($dropbox)->{'revision'}; movefile Arguments: $dropbox, $source, $target Function moves file or directory from one location to another. Metadata for moved file can be accessed using "metadata" function. movefile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!; say 'Created backup with size ', metadata($dropbox)->{'size'}; deletefile Arguments: $dropbox, $path Function deletes file or folder at specified path. Metadata for deleted item is accessible via "metadata" function. deletefile($dropbox, '/data/2012.dat.bak') or die $!; say 'Deleted backup with last modification ', metadata($dropbox)->{'modification'}; createfolder Arguments: $dropbox, $path Function creates folder at specified path. Metadata for created folder is accessible via "metadata" function. createfolder($dropbox, '/data/backups') or die $!; say 'Created folder at path ', metadata($dropbox)->{'path'}; SEE ALSO Furl, Furl::HTTP, WebService::Dropbox, Dropbox API AUTHOR Alexander Nazarov COPYRIGHT AND LICENSE Copyright 2013-2016 Alexander Nazarov This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. File-Dropbox-0.7/lib000755000765000024 012756424432 13603 5ustar00n4kzstaff000000000000File-Dropbox-0.7/lib/File000755000765000024 012756424432 14462 5ustar00n4kzstaff000000000000File-Dropbox-0.7/lib/File/Dropbox.pm000444000765000024 4276412756424432 16627 0ustar00n4kzstaff000000000000package File::Dropbox; use strict; use warnings; use feature ':5.10'; use base qw{ Tie::Handle Exporter }; use Symbol; use JSON; use Errno qw{ ENOENT EISDIR EINVAL EPERM EACCES EAGAIN ECANCELED EFBIG }; use Fcntl qw{ SEEK_CUR SEEK_SET SEEK_END }; use Furl; use IO::Socket::SSL; use Net::DNS::Lite; our $VERSION = 0.7; our @EXPORT_OK = qw{ contents metadata putfile movefile copyfile createfolder deletefile }; my $hosts = { content => 'api-content.dropbox.com', api => 'api.dropbox.com', }; my $version = 1; my $header1 = join ', ', 'OAuth oauth_version="1.0"', 'oauth_signature_method="PLAINTEXT"', 'oauth_consumer_key="%s"', 'oauth_token="%s"', 'oauth_signature="%s&%s"'; my $header2 = 'Bearer %s'; sub new { my $self = Symbol::gensym; tie *$self, __PACKAGE__, my $this = { @_[1 .. @_ - 1] }; *$self = $this; return $self; } # new sub TIEHANDLE { my $self = bless $_[1], ref $_[0] || $_[0]; $self->{'chunk'} //= 4 << 20; $self->{'root'} //= 'sandbox'; die 'Unexpected root value' unless $self->{'root'} =~ m{^(?:drop|sand)box$}; $self->{'furl'} = Furl->new( timeout => 10, inet_aton => \&Net::DNS::Lite::inet_aton, ssl_opts => { SSL_verify_mode => SSL_VERIFY_PEER(), }, %{ $self->{'furlopts'} //= {} }, ); $self->{'closed'} = 1; $self->{'length'} = 0; $self->{'position'} = 0; $self->{'mode'} = ''; $self->{'buffer'} = ''; return $self; } # TIEHANDLE sub READ { my ($self, undef, $length, $offset) = @_; undef $!; die 'Read is not supported on this handle' if $self->{'mode'} ne '<'; substr($_[1] //= '', $offset // 0) = '', return 0 if $self->EOF(); my $furl = $self->{'furl'}; my $url = 'https://'; $url .= join '/', $hosts->{'content'}, $version; $url .= join '/', '/files', $self->{'root'}, $self->{'path'}; my $response = $furl->get($url, [ Range => sprintf('bytes=%i-%i', $self->{'position'}, $self->{'position'} + ($length || 1)), @{ &__headers__ }, ]); return $self->__error__($response) if $response->code != 206; my $meta = $response->header('X-Dropbox-Metadata'); my $bytes = $response->header('Content-Length'); $self->{'position'} += $bytes > $length? $length : $bytes; substr($_[1] //= '', $offset // 0) = substr $response->content(), 0, $length; return $bytes; } # READ sub READLINE { my ($self) = @_; my $length; undef $!; die 'Readline is not supported on this handle' if $self->{'mode'} ne '<'; if ($self->EOF()) { return if wantarray; # Special case: slurp mode + scalar context + empty file # return '' for first call and undef for subsequent return '' unless $self->{'eof'} or defined $/; $self->{'eof'} = 1; return undef; } { $length = length $self->{'buffer'}; if (not wantarray and $length and defined $/) { my $position = index $self->{'buffer'}, $/; if (~$position) { $self->{'position'} += ($position += length $/); return substr $self->{'buffer'}, 0, $position, ''; } } local $self->{'position'} = $self->{'position'} + $length; my $bytes = $self->READ($self->{'buffer'}, $self->{'chunk'}, $length); return if $!; redo if not $length or $bytes; } $length = length $self->{'buffer'}; if ($length) { # Multiline if (wantarray and defined $/) { $self->{'position'} += $length; my ($position, $length) = (0, length $/); my @lines; foreach ($self->{'buffer'}) { while (~(my $offset = index $_, $/, $position)) { $offset += $length; push @lines, substr $_, $position, $offset - $position; $position = $offset; } push @lines, substr $_, $position if $position < length; $_ = ''; } return @lines; } # Slurp or last chunk $self->{'position'} += $length; return substr $self->{'buffer'}, 0, $length, ''; } return undef; } # READLINE sub SEEK { my ($self, $position, $whence) = @_; undef $!; die 'Seek is not supported on this handle' if $self->{'mode'} ne '<'; $self->{'buffer'} = ''; delete $self->{'eof'}; if ($whence == SEEK_SET) { $self->{'position'} = $position } elsif ($whence == SEEK_CUR) { $self->{'position'} += $position } elsif ($whence == SEEK_END) { $self->{'position'} = $self->{'length'} + $position } else { $! = EINVAL; return 0; } $self->{'position'} = 0 if $self->{'position'} < 0; return 1; } # SEEK sub TELL { my ($self) = @_; die 'Tell is not supported on this handle' if $self->{'mode'} ne '<'; return $self->{'position'}; } # TELL sub WRITE { my ($self, $buffer, $length, $offset) = @_; undef $!; die 'Write is not supported on this handle' if $self->{'mode'} ne '>'; die 'Append-only writes supported' if $offset and $offset != $self->{'offset'} + $self->{'length'}; $self->{'offset'} //= $offset; $self->{'buffer'} .= $buffer; $self->{'length'} += $length; $self->__flush__() or return 0 while $self->{'length'} >= $self->{'chunk'}; return 1; } # WRITE sub CLOSE { my ($self) = @_; undef $!; return 1 if $self->{'closed'}; my $mode = $self->{'mode'}; if ($mode eq '>') { if ($self->{'length'} or not $self->{'upload_id'}) { do { @{ $self }{qw{ closed mode }} = (1, '') and return 0 unless $self->__flush__(); } while length $self->{'buffer'}; } } $self->{'closed'} = 1; $self->{'mode'} = ''; return $self->__flush__() if $mode eq '>'; return 1; } # CLOSE sub OPEN { my ($self, $mode, $file) = @_; undef $!; ($mode, $file) = $mode =~ m{^([<>]?)(.*)$}s unless $file; $mode ||= '<'; $mode = '<' if $mode eq 'r'; $mode = '>' if $mode eq 'a' or $mode eq 'w'; die 'Unsupported mode' unless $mode eq '<' or $mode eq '>'; $self->CLOSE() unless $self->{'closed'}; $self->{'length'} = 0; $self->{'position'} = 0; $self->{'buffer'} = ''; delete $self->{'offset'}; delete $self->{'revision'}; delete $self->{'upload_id'}; delete $self->{'meta'}; delete $self->{'eof'}; $self->{'path'} = $file or die 'Path required'; return 0 if $mode eq '<' and not $self->__meta__(); $self->{'mode'} = $mode; $self->{'closed'} = 0; return 1; } # OPEN sub EOF { my ($self) = @_; die 'Eof is not supported on this handle' if $self->{'mode'} ne '<'; return $self->{'position'} >= $self->{'length'}; } # EOF sub BINMODE { 1 } sub __headers__ { return [ 'Authorization', $_[0]->{'oauth2'}? sprintf $header2, $_[0]->{'access_token'}: sprintf $header1, @{ $_[0] }{qw{ app_key access_token app_secret access_secret }}, ]; } sub __flush__ { my ($self) = @_; my $furl = $self->{'furl'}; my $url; $url = 'https://'; $url .= join '/', $hosts->{'content'}, $version; $url .= join '/', '/commit_chunked_upload', $self->{'root'}, $self->{'path'} if $self->{'closed'}; $url .= '/chunked_upload' unless $self->{'closed'}; $url .= '?'; $url .= join '=', 'upload_id', $self->{'upload_id'} if $self->{'upload_id'}; $url .= '&' if $self->{'upload_id'}; $url .= join '=', 'offset', $self->{'offset'} || 0 unless $self->{'closed'}; my $response; unless ($self->{'closed'}) { use bytes; my $buffer = substr $self->{'buffer'}, 0, $self->{'chunk'}, ''; my $length = length $buffer; $self->{'length'} -= $length; $self->{'offset'} += $length; $response = $furl->put($url, &__headers__, $buffer); } else { $response = $furl->post($url, &__headers__); } return $self->__error__($response) if $response->code != 200; $self->{'meta'} = from_json($response->content()) if $self->{'closed'}; unless ($self->{'upload_id'}) { $response = from_json($response->content()); $self->{'upload_id'} = $response->{'upload_id'}; } return 1; } # __flush__ sub __meta__ { my ($self) = @_; my ($url, $meta); my $furl = $self->{'furl'}; $url = 'https://'; $url .= join '/', $hosts->{'api'}, $version; $url .= join '/', '/metadata', $self->{'root'}, $self->{'path'}; $url .= '?hash='. delete $self->{'hash'} if $self->{'hash'}; my $response = $furl->get($url, &__headers__); my $code = $response->code(); if ($code == 200) { $meta = $self->{'meta'} = from_json($response->content()); # XXX: Dropbox returns metadata for recently deleted files if ($meta->{'is_deleted'}) { $! = ENOENT; return 0; } } elsif ($code != 304) { return $self->__error__($response); } if ($meta->{'is_dir'}) { $! = EISDIR; return 0; } $self->{'revision'} = $meta->{'rev'}; $self->{'length'} = $meta->{'bytes'}; return 1; } # __meta__ sub __fileops__ { my ($type, $handle, $source, $target) = @_; my $self = *$handle{'HASH'}; my $furl = $self->{'furl'}; my ($url, @arguments); $url = 'https://'; $url .= join '/', $hosts->{'api'}, $version; $url .= join '/', '/fileops', $type; if ($type eq 'move' or $type eq 'copy') { @arguments = ( from_path => $source, to_path => $target, ); } else { @arguments = ( path => $source, ); } push @arguments, root => $self->{'root'}; my $response = $furl->post($url, $self->__headers__(), \@arguments); return $self->__error__($response) if $response->code != 200; $self->{'meta'} = from_json($response->content()); return 1; } # __fileops__ sub __error__ { my ($self, $response) = @_; my $code = $response->code(); if ($code == 400) { $! = EINVAL; } elsif ($code == 401 or $code == 403) { $! = EACCES; } elsif ($code == 404) { $! = ENOENT; return 0; } elsif ($code == 406) { $! = EPERM; return 0; } elsif ($code == 500 and $response->content() =~ m{\A(?:Cannot|Failed)}) { $! = ECANCELED; } elsif ($code == 503) { $self->{'meta'} = { retry => $response->header('Retry-After') }; $! = EAGAIN; } elsif ($code == 507) { $! = EFBIG; } else { die join ' ', $code, $response->decoded_content(); } return 0; } # __error__ sub contents ($;$$) { my ($handle, $path, $hash) = @_; die 'GLOB reference expected' unless ref $handle eq 'GLOB'; *$handle->{'hash'} = $hash if $hash; if (open $handle, '<', $path || '/' or $! != EISDIR) { delete *$handle->{'meta'}; return; } undef $!; return @{ *$handle->{'meta'}{'contents'} }; } # contents sub putfile ($$$) { my ($handle, $path, $data) = @_; die 'GLOB reference expected' unless ref $handle eq 'GLOB'; close $handle or return 0; my $self = *$handle{'HASH'}; my $furl = $self->{'furl'}; my ($url, $length); $url = 'https://'; $url .= join '/', $hosts->{'content'}, $version; $url .= join '/', '/files_put', $self->{'root'}, $path; { use bytes; $length = length $data; } my $response = $furl->put($url, $self->__headers__(), $data); return $self->__error__($response) if $response->code != 200; $self->{'path'} = $path; $self->{'meta'} = from_json($response->content()); return 1; } # putfile sub movefile ($$$) { __fileops__('move', @_) } sub copyfile ($$$) { __fileops__('copy', @_) } sub deletefile ($$) { __fileops__('delete', @_) } sub createfolder ($$) { __fileops__('create_folder', @_) } sub metadata ($) { my ($handle) = @_; die 'GLOB reference expected' unless ref $handle eq 'GLOB'; my $self = *$handle{'HASH'}; die 'Meta is unavailable for incomplete upload' if $self->{'mode'} eq '>'; return $self->{'meta'}; } # metadata =head1 NAME File::Dropbox - Convenient and fast Dropbox API abstraction =head1 SYNOPSIS use File::Dropbox; use Fcntl; # Application credentials my %app = ( oauth2 => 1, access_token => $access_token, ); my $dropbox = File::Dropbox->new(%app); # Open file for writing open $dropbox, '>', 'example' or die $!; while (<>) { # Upload data using 4MB chunks print $dropbox $_; } # Commit upload (optional, close will be called on reopen) close $dropbox or die $!; # Open for reading open $dropbox, '<', 'example' or die $!; # Download and print to STDOUT # Buffered, default buffer size is 4MB print while <$dropbox>; # Reset file position seek $dropbox, 0, Fcntl::SEEK_SET; # Get first character (unbuffered) say getc $dropbox; close $dropbox; =head1 DESCRIPTION C provides high-level Dropbox API abstraction based on L. Code required to get C and C for signed OAuth 1.0 requests or C for OAuth 2.0 requests is not included in this module. To get C and C you need to register your application with Dropbox. At this moment Dropbox API is not fully supported, C covers file read/write and directory listing methods. If you need full API support take look at L. C main purpose is not 100% API coverage, but simple and high-performance file operations. Due to API limitations and design you can not do read and write operations on one file at the same time. Therefore handle can be in read-only or write-only state, depending on last call to L. Supported functions for read-only state are: L, L, L, L, L, L, L, L, L. For write-only state: L, L, L, L, L, L. All API requests are done using L module. For more accurate timeouts L is used, as described in L. Furl settings can be overriden using C. =head1 METHODS =head2 new my $dropbox = File::Dropbox->new( access_secret => $access_secret, access_token => $access_token, app_secret => $app_secret, app_key => $app_key, chunk => 8 * 1024 * 1024, root => 'dropbox', furlopts => { timeout => 20 } ); my $dropbox = File::Dropbox->new( access_token => $access_token, oauth2 => 1 ); Constructor, takes key-value pairs list =over =item access_secret OAuth 1.0 access secret =item access_token OAuth 1.0 access token or OAuth 2.0 access token =item app_secret OAuth 1.0 app secret =item app_key OAuth 1.0 app key =item oauth2 OAuth 2.0 switch, defaults to false. =item chunk Upload chunk size in bytes. Also buffer size for C. Optional. Defaults to 4MB. =item root Access type, C for app-folder only access and C for full access. =item furlopts Parameter hash, passed to L constructor directly. Default options timeout => 10, inet_aton => \&Net::DNS::Lite::inet_aton, ssl_opts => { SSL_verify_mode => SSL_VERIFY_PEER(), } =back =head1 FUNCTIONS All functions are not exported by default but can be exported on demand. use File::Dropbox qw{ contents metadata putfile }; First argument for all functions should be GLOB reference, returned by L. =head2 contents Arguments: $dropbox [, $path] Function returns list of hashrefs representing directory content. Hash fields described in L. C<$path> defaults to C. If there is unfinished chunked upload on handle, it will be commited. foreach my $file (contents($dropbox, '/data')) { next if $file->{'is_dir'}; say $file->{'path'}, ' - ', $file->{'bytes'}; } =head2 metadata Arguments: $dropbox Function returns stored metadata for read-only handle, closed write handle or after call to L or L. open $dropbox, '<', '/data/2013.dat' or die $!; my $meta = metadata($dropbox); if ($meta->{'bytes'} > 1024) { # Do something } =head2 putfile Arguments: $dropbox, $path, $data Function is useful for uploading small files (up to 150MB possible) in one request (at least two API requests required for chunked upload, used in open-write-close sequence). If there is unfinished chunked upload on handle, it will be commited. local $/; open my $data, '<', '2012.dat' or die $!; putfile($dropbox, '/data/2012.dat', <$data>) or die $!; say 'Uploaded ', metadata($dropbox)->{'bytes'}, ' bytes'; close $data; =head2 copyfile Arguments: $dropbox, $source, $target Function copies file or directory from one location to another. Metadata for copy can be accessed using L function. copyfile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!; say 'Created backup with revision ', metadata($dropbox)->{'revision'}; =head2 movefile Arguments: $dropbox, $source, $target Function moves file or directory from one location to another. Metadata for moved file can be accessed using L function. movefile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!; say 'Created backup with size ', metadata($dropbox)->{'size'}; =head2 deletefile Arguments: $dropbox, $path Function deletes file or folder at specified path. Metadata for deleted item is accessible via L function. deletefile($dropbox, '/data/2012.dat.bak') or die $!; say 'Deleted backup with last modification ', metadata($dropbox)->{'modification'}; =head2 createfolder Arguments: $dropbox, $path Function creates folder at specified path. Metadata for created folder is accessible via L function. createfolder($dropbox, '/data/backups') or die $!; say 'Created folder at path ', metadata($dropbox)->{'path'}; =head1 SEE ALSO L, L, L, L =head1 AUTHOR Alexander Nazarov =head1 COPYRIGHT AND LICENSE Copyright 2013-2016 Alexander Nazarov This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; File-Dropbox-0.7/t000755000765000024 012756424432 13300 5ustar00n4kzstaff000000000000File-Dropbox-0.7/t/01-basic.t000444000765000024 263112756424432 15123 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More tests => 5; BEGIN { use_ok 'File::Dropbox'; use_ok 'Test::Common'; } my $dropbox; eval { $dropbox = File::Dropbox->new(root => 'frfrbox') }; like $@, qr{Unexpected root value}, 'Root parameter validation'; $dropbox = File::Dropbox->new(); subtest Constructor => sub { is ref $dropbox, 'GLOB', 'Constructor returned GLOB reference'; isa_ok *$dropbox{'IO'}, 'IO::Handle', 'GLOB contains IO handle'; isa_ok *$dropbox{'HASH'}, 'File::Dropbox', 'GLOB contais tied object'; is binmode($dropbox), 1, 'Binmode works'; }; subtest Self => sub { my $self = *$dropbox{'HASH'}; isa_ok $self, 'Tie::Handle', 'Tied object inherits Tie::Handle'; isa_ok $self, 'Exporter', 'Tied object inherits Exporter'; is $self->{'chunk'}, 4 * 1024 * 1024, 'Chunk size is set'; is $self->{'root'}, 'sandbox', 'Root is set'; is $self->{'mode'}, '', 'Mode is not set'; is $self->{'closed'}, 1, 'Handle is not opened'; is $self->{'position'}, 0, 'Handle position is 0'; is $self->{'length'}, 0, 'Buffer length is 0'; is $self->{'buffer'}, '', 'Buffer is empty'; can_ok $self, qw{ READ WRITE READLINE SEEK TELL OPEN CLOSE EOF BINMODE contents putfile metadata movefile deletefile createfolder copyfile }; is_deeply \@File::Dropbox::EXPORT_OK, [qw{ contents metadata putfile movefile copyfile createfolder deletefile }], '@EXPORT_OK is set'; }; File-Dropbox-0.7/t/02-open.t000444000765000024 371012756424432 15003 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More tests => 15; use File::Dropbox; use Test::Common ':all'; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(); my $file = $path. '/'. time; sub is_closed { subtest Closed => sub { no warnings 'void'; eval { tell $dropbox }; like $@, qr{Tell is not supported on this handle}, 'Tell failed on unopened handle'; eval { seek $dropbox, 0, SEEK_CUR }; like $@, qr{Seek is not supported on this handle}, 'Seek failed on unopened handle'; eval { read $dropbox, $_, 16 }; like $@, qr{Read is not supported on this handle}, 'Read failed on unopened handle'; eval { readline $dropbox }; like $@, qr{Readline is not supported on this handle}, 'Readline failed on unopened handle'; eval { print $dropbox 'test' }; like $@, qr{Write is not supported on this handle}, 'Write failed on unopened handle'; eval { eof $dropbox }; like $@, qr{Eof is not supported on this handle}, 'Eof failed on unopened handle'; my $self = *$dropbox{'HASH'}; ok !$self->{'mode'}, 'Mode is not set'; ok !$self->{'buffer'}, 'Buffer is empty'; ok !$self->{'length'}, 'Length is not set'; ok !$self->{'position'}, 'Position is not set'; ok $self->{'closed'}, 'Closed flag is set'; }; } # is_closed SKIP: { skip 'DROPBOX_AUTH is not set or has wrong value', 14 unless keys %$app; is_closed(); # Try to open not existing file for reading errn { open $dropbox, '<', $file } ENOENT, 'Failed to open not existing file'; is_closed(); # Try to open it for writing okay { open $dropbox, '>', $file } 'File opened for writing'; # Try to open directory for reading errn { open $dropbox, '<', $path } EISDIR, 'Failed to read directory'; # Open file for reading again okay { open $dropbox, '<', $file } 'Empty file created'; # Check end and close okay { eof $dropbox } 'File is empty'; okay { close $dropbox } 'File is closed'; } # SKIP is_closed(); File-Dropbox-0.7/t/03-write.t000444000765000024 544512756424432 15204 0ustar00n4kzstaff000000000000use strict; use warnings; use feature 'say'; use lib 't/lib'; use Test::More; use Test::Common qw{ EINVAL :func }; use File::Dropbox qw{ metadata deletefile }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app, chunk => 16 * 1024); my $path = base(); my $file = $path. '/'. time; my $counter = 0; my $conflict; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 67; package Furl; no warnings 'redefine'; my $request = UNIVERSAL::can(__PACKAGE__, 'request'); # Request counter *request = sub { ++$counter; goto &$request; }; package main; sub cntr ($) { is $counter, $_[0], sprintf 'Completed %i requests', $_[0] } # Try to open directory for writing okay { open $dropbox, '>', $path } 'Path opened'; okay { say $dropbox 'Test directory for writing' } 'Test string written'; # XXX: new behaviour of Dropbox API, file created with (conflicted copy) in name okay { close $dropbox } 'Commited'; my $conflicted = (metadata $dropbox)->{'path'}; like $conflicted, qr{$path}, 'Path for conflicted copy matches'; cntr 2; # Write plain file okay { open $dropbox, '>', $file } 'File opened for writing'; okay { print $dropbox 'A' x 1024 } '1k'; okay { print $dropbox 'B' x 1024 } '2k'; okay { print $dropbox 'C' x 1024 } '3k'; okay { print $dropbox 'D' x 1024 } '4k'; okay { close $dropbox } 'Committed'; cntr 4; # Check file content okay { open $dropbox, '<', $file } 'File opened for reading'; is readline $dropbox, join('', 'A' x 1024, 'B' x 1024, 'C' x 1024, 'D' x 1024), 'Content is okay'; cntr 6; # Rewrite file okay { open $dropbox, '>', $file } 'File opened for writing'; okay { printf $dropbox '%s', 'E' x 4096 } '4k'; okay { printf $dropbox '%s', 'F' x 4096 } '8k'; cntr 6; # Check file content okay { open $dropbox, '<', $file } 'File opened for reading'; cntr 9; is readline $dropbox, join('', 'E' x 4096, 'F' x 4096), 'Content is okay'; cntr 10; # Multipart upload okay { open $dropbox, '>', $file } 'File opened for writing'; okay { print $dropbox 'G' x 4096 } '4k'; okay { print $dropbox 'H' x 8192 } '12k'; okay { print $dropbox 'I' x 8192 } '20k'; okay { close $dropbox } 'Committed'; cntr 13; # Check file content okay { open $dropbox, '<', $file } 'File opened for reading'; cntr 14; is readline $dropbox, join('', 'G' x 4096, 'H' x 8192, 'I' x 8192), 'Content is okay'; cntr 16; # Truncate file okay { open $dropbox, '>', $file } 'File opened for writing'; okay { close $dropbox } 'Committed'; cntr 18; # Check file content okay { open $dropbox, '<', $file } 'File opened for reading'; cntr 19; is readline $dropbox, undef, 'Content is okay'; okay { close $dropbox } 'All done'; cntr 19; okay { deletefile $dropbox, $conflicted } 'Remove conflicted copy'; File-Dropbox-0.7/t/04-read.t000444000765000024 603612756424432 14763 0ustar00n4kzstaff000000000000use strict; use warnings; use feature 'say'; use lib 't/lib'; use Test::More; use Test::Common qw{ EINVAL :func :fcntl }; use File::Dropbox; my $app = conf(); my $dropbox = File::Dropbox->new(%$app, chunk => 4096); my $path = base(); my $file = $path. '/'. time; my $data; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 78; # Write plain file okay { open $dropbox, '>', $file } 'File opened for writing'; okay { print $dropbox 'A' x 1024 } '1k'; okay { print $dropbox 'B' x 1024 } '2k'; okay { print $dropbox 'C' x 1024 } '3k'; okay { print $dropbox 'D' x 1024 } '4k'; okay { print $dropbox 'E' x 1024 } '5k'; okay { print $dropbox 'F' x 1024 } '6k'; okay { close $dropbox } 'Committed'; # Seek test okay { open $dropbox, '<', $file } 'File opened for reading'; okay { seek $dropbox, 1024, SEEK_SET } 'Seek absolute'; okay { read $dropbox, $data, 2048 } 'Read 2k from file'; is $data, join('', 'B' x 1024, 'C' x 1024), 'Content is okay'; okay { read $dropbox, $data, 2048 } 'Read 2k from file'; is $data, join('', 'D' x 1024, 'E' x 1024), 'Content is okay'; okay { seek $dropbox, -512, SEEK_CUR } 'Seek relative'; okay { read $dropbox, $data, 1024 } 'Read 1k from file'; is $data, join('', 'E' x 512, 'F' x 512), 'Content is okay'; is tell $dropbox, 5 * 1024 + 512, 'Right position is set'; okay { read $dropbox, $data, 1024 } 'Read 1k from file'; is tell $dropbox, 6 * 1024, 'Right position is set'; is $data, join('', 'F' x 512), 'Content is okay'; okay { eof $dropbox } 'File end reached'; okay { seek $dropbox, -1024, SEEK_END } 'Seek relative to end'; okay { seek $dropbox, -1024, SEEK_END } 'Seek relative to end'; is tell $dropbox, 5 * 1024, 'Right position is set'; okay { read $dropbox, $data, 1024 } 'Read 1k from file'; is $data, join('', 'F' x 1024), 'Content is okay'; errn { read $dropbox, $data, 1024 } 0, 'Read 1k from file end'; is $data, '', 'No content'; is tell $dropbox, 6 * 1024, 'Right position is set'; okay { seek $dropbox, 1024, SEEK_END } 'Seek beyond end'; errn { read $dropbox, $data, 1024 } 0, 'Read 1k from file end'; is $data, '', 'No content'; is tell $dropbox, 7 * 1024, 'Right position is set'; okay { seek $dropbox, -1024, SEEK_SET } 'Seek before start'; okay { read $dropbox, $data, 1024 } 'Read 1k from file start'; is $data, 'A' x 1024, 'Right content'; is tell $dropbox, 1024, 'Right position is set'; okay { seek $dropbox, 1023, SEEK_SET } 'Seek to 1023'; is getc $dropbox, 'A', 'Get A character'; is getc $dropbox, 'B', 'Get B character'; is getc $dropbox, 'B', 'Get B character'; is tell $dropbox, 1026, 'Right position is set'; okay { open $dropbox, '<', $file } 'File reopened for reading'; is tell $dropbox, 0, 'Right position is set'; ok !eof $dropbox, 'Not at end of file'; okay { seek $dropbox, 0, SEEK_CUR } 'Dummy seek'; is tell $dropbox, 0, 'Right position is set'; ok !eof $dropbox, 'Not at end of file'; okay { close $dropbox } 'All done'; File-Dropbox-0.7/t/05-meta.t000444000765000024 447312756424432 15002 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ EISDIR :func }; use File::Dropbox 'metadata'; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(); my $file = $path. '/'. time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 31; # Closed handle is metadata $dropbox, undef, 'No metadata for closed handle'; eval { metadata $app }; like $@, qr{GLOB reference expected}, 'Function called on wrong reference'; # Create empty file okay { open $dropbox, '>', $file } 'File opened for writing'; eval { metadata $dropbox }; like $@, qr{Meta is unavailable}, 'No metadata for handle in write mode'; okay { close $dropbox } 'File committed'; # Get meta from closed handle my $meta = metadata $dropbox; # XXX: Remove id from meta (not documented field) my $id = delete $meta->{'id'}; is ref $meta, 'HASH', 'Got hashref from metadata()'; is $meta->{'bytes'}, 0, 'File size is set'; is $meta->{'is_dir'}, JSON::false, 'File type is set'; is $meta->{'path'}, '/'. $file, 'File path is set'; ok exists $meta->{'rev'}, 'Rev number is present'; ok exists $meta->{'root'}, 'File root is present'; ok exists $meta->{'revision'}, 'Rev hash is present'; ok exists $meta->{'modified'}, 'File mtime is present'; # Reopen file for reading okay { open $dropbox, '<', $file } 'File opened for reading'; my $meta2 = metadata $dropbox; is_deeply $meta, $meta2, 'Meta for write mode matches meta for read mode'; # Open directory errn { open $dropbox, '<', $path } EISDIR, 'Directory opened'; $meta = metadata $dropbox; # Get contents my $contents = $meta->{'contents'}; is ref $meta, 'HASH', 'Metadata is hash'; is ref $contents, 'ARRAY', 'Directory files are in array'; ok !(grep { ref $_ ne 'HASH' } @$contents), 'Metadata for each file is hash'; is $meta->{'bytes'}, 0, 'File size is set'; is $meta->{'is_dir'}, JSON::true, 'File type is set'; is $meta->{'path'}, '/'. $path, 'File path is set'; ok exists $meta->{'rev'}, 'Rev number is present'; ok exists $meta->{'hash'}, 'Cache hash is present'; ok exists $meta->{'root'}, 'File root is present'; ok exists $meta->{'revision'}, 'Rev hash is present'; ok exists $meta->{'modified'}, 'File mtime is present'; File-Dropbox-0.7/t/06-putfile.t000444000765000024 157512756424432 15525 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func }; use File::Dropbox qw{ putfile metadata }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $file = join '/', base(), time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 7; eval { putfile $app, $file, 'ABCD' }; like $@, qr{GLOB reference expected}, 'Function called on wrong reference'; # Normal upload okay { putfile $dropbox, $file, 'A' x 1024 } 'Put 1k file'; # Get meta from closed handle my $meta = metadata $dropbox; okay { open $dropbox, '<', $file } 'Open file for reading'; # Get meta from opened handle my $meta2 = metadata $dropbox; # Compare is_deeply $meta, $meta2, 'Metadata matches'; # Read file my $data = readline $dropbox; # Check content is $data, join('', 'A' x 1024), 'Content is okay'; File-Dropbox-0.7/t/07-contents.t000444000765000024 333012756424432 15702 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func ENOENT }; use File::Dropbox qw{ putfile metadata contents }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(). '/contents/'; my $file = $path. '/'. time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 14; eval { contents $app }; like $@, qr{GLOB reference expected}, 'Function called on wrong reference'; # Create test file and directory okay { putfile $dropbox, $file, 'A' x 1024 } 'Put 1k file'; # Get metadata and directory contents my $filemeta = metadata $dropbox; my @contents = contents $dropbox, $path; my $meta = metadata $dropbox; my $hash = $meta->{'hash'}; # FIXME: Looks like dropbox API always returns 'dropbox' in directory listing local $contents[0]{'root'} = 'app_folder'; is scalar @contents, 1, 'One file is present in directory'; is_deeply $contents[0], $filemeta, 'Contents meta matches file meta'; is_deeply \@contents, $meta->{'contents'}, 'Meta contents are equal'; ## Supply hash @contents = contents $dropbox, $path, $hash; $meta = metadata $dropbox; is scalar @contents, 0, 'Empty list returned'; is $meta, undef, 'Metadata is undefined'; # Try to get contents on file @contents = contents $dropbox, $file; $meta = metadata $dropbox; is scalar @contents, 0, 'Empty list returned'; is $meta, undef, 'Metadata is undefined'; # Get contents from / @contents = contents $dropbox; $meta = metadata $dropbox; ok scalar @contents, 'Not empty list returned'; is ref $meta, 'HASH', 'Metadata is set'; errn { contents $dropbox, $path. '/not_exists' } ENOENT, 'Open invalid directory'; File-Dropbox-0.7/t/08-timeout.t000444000765000024 133212756424432 15534 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func ECANCELED }; use File::Dropbox; my $app = conf(); my $dropbox = File::Dropbox->new(%$app, furlopts => { timeout => 1, }); unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } unless (exists $ENV{'DROPBOX_TIMEOUT'}) { plan skip_all => 'DROPBOX_TIMEOUT is not set'; exit; } plan tests => 10; errn { open $dropbox, 'r', time } ECANCELED, 'Timeout on open'; okay { open $dropbox, 'w', time } 'File opened for writing'; okay { print $dropbox 'A' x 4096 } '4k'; errn { print $dropbox 'A' x (8 << 20); } ECANCELED, 'Timeout on write'; errn { close $dropbox; } ECANCELED, 'Timeout on close'; File-Dropbox-0.7/t/09-movefile.t000444000765000024 227712756424432 15666 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func :errno }; use File::Dropbox qw{ putfile movefile copyfile }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(); my $filea = $path. '/e/'. time; my $fileb = $path. '/f/'. time; my $filec = $path. '/g/'. time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 17; okay { putfile $dropbox, $filea, 'X' x 1024 } 'Put 1k file'; okay { movefile $dropbox, $filea, $fileb } 'Rename file'; okay { open $dropbox, '<', $fileb } 'Open renamed file for reading'; # Read file my $data = readline $dropbox; # Check content is $data, join('', 'X' x 1024), 'Content is okay'; # First file not exists anymore errn { open $dropbox, '<', $filea; } ENOENT, 'Failed to read from not existing file'; # Move not existing file errn { movefile $dropbox, $filea, $fileb; } ENOENT, 'Failed to move not existing file'; okay { movefile $dropbox, $fileb, $fileb; } 'Moving to same name does nothing'; okay { copyfile $dropbox, $fileb, $filec } 'Copy created'; errn { movefile $dropbox, $filec, $fileb; } EACCES, 'Failed override another file'; File-Dropbox-0.7/t/10-copyfile.t000444000765000024 236512756424432 15660 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func :errno }; use File::Dropbox qw{ putfile copyfile movefile }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(); my $filea = $path. '/i/'. time; my $fileb = $path. '/j/'. time; my $filec = $path. '/k/'. time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 16; okay { putfile $dropbox, $filea, 'Y' x 1024 } 'Put 1k file'; okay { copyfile $dropbox, $filea, $fileb } 'Create copy'; okay { open $dropbox, '<', $fileb } 'Open target for reading'; # Read file my $data = readline $dropbox; # Check content is $data, join('', 'Y' x 1024), 'Content is okay'; # Source file remains okay { open $dropbox, '<', $filea } 'Open source for reading'; # Read file $data = readline $dropbox; # Check content is $data, join('', 'Y' x 1024), 'Content is okay'; # Copy not existing file errn { copyfile $dropbox, $filec, $filea; } ENOENT, 'Failed to copy not existing file'; # Copy file to itself okay { movefile $dropbox, $fileb, $fileb } 'File copied to same name'; # Try overwrite file errn { movefile $dropbox, $fileb, $filea; } EACCES, 'Failed to overwrite one file with another'; File-Dropbox-0.7/t/11-createfolder.t000444000765000024 146212756424432 16503 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func :errno }; use File::Dropbox qw{ metadata createfolder deletefile }; my $app = conf(); my $dropbox = File::Dropbox->new(%$app); my $path = base(); my $folder = $path. '/z/'. time; unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 9; # Check folder existence errn { open $dropbox, '<', $folder } ENOENT, 'Folder not exists'; # Create folder okay { createfolder $dropbox, $folder } 'Folder created'; # Check returned path is metadata($dropbox)->{'path'}, '/'. $folder, 'Path is okay'; # Try to create folder again errn { createfolder $dropbox, $folder } EACCES, 'Folder exists already'; # Delete folder okay { deletefile $dropbox, $folder } 'Folder deleted'; File-Dropbox-0.7/t/99-clean.t000444000765000024 71312756424432 15124 0ustar00n4kzstaff000000000000use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Common qw{ :func ENOENT }; use File::Dropbox qw{ deletefile }; my $app = conf(); my $path = base(); my $dropbox = File::Dropbox->new(%$app); unless (keys %$app) { plan skip_all => 'DROPBOX_AUTH is not set or has wrong value'; exit; } plan tests => 4; okay { deletefile $dropbox, $path } 'Test directory removed'; errn { deletefile $dropbox, $path } ENOENT, 'Removed already'; File-Dropbox-0.7/t/lib000755000765000024 012756424432 14046 5ustar00n4kzstaff000000000000File-Dropbox-0.7/t/lib/Test000755000765000024 012756424432 14765 5ustar00n4kzstaff000000000000File-Dropbox-0.7/t/lib/Test/Common.pm000444000765000024 177312756424432 16720 0ustar00n4kzstaff000000000000package Test::Common; use strict; use warnings; use Exporter 'import'; use Test::More; use Fcntl qw{ SEEK_CUR SEEK_SET SEEK_END }; use Errno qw{ ENOENT EISDIR EINVAL EPERM EACCES ECANCELED }; our %EXPORT_TAGS = ( fcntl => [qw{ SEEK_CUR SEEK_SET SEEK_END }], errno => [qw{ ENOENT EISDIR EINVAL EPERM EACCES ECANCELED }], func => [qw{ okay errn conf base }], ); our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; $EXPORT_TAGS{'all'} = \@EXPORT_OK; sub okay (&$) { local $\; my $result = &{ $_[0] }; ok $result, $_[1]; ok !$!, 'Error is not set'; } # okay sub errn (&$$) { my $result = &{ $_[0] }; ok !$result, $_[2]; is int $!, $_[1], 'Error is set'; } # errn sub conf { my %app; if (exists $ENV{'DROPBOX_AUTH'}) { if (~index $ENV{'DROPBOX_AUTH'}, ':') { @app{qw{ app_key app_secret access_token access_secret }} = split ':', $ENV{'DROPBOX_AUTH'} || ''; } else { @app{qw{ oauth2 access_token }} = (1, $ENV{'DROPBOX_AUTH'}); } } return \%app; } # conf sub base { 'test' } 1;