#!%PERL% -w
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

package Vhffs::Panel::Avatar;

use strict;
use utf8;

use Vhffs::Functions;
use Digest::MD5 qw(md5 md5_hex md5_base64);

sub store_avatar
{
	my $vhffs = shift;
	my $object = shift;
    my $file = shift;
	my $type = shift;
    my $config = $vhffs->get_config;
    my $datadir = $config->get_datadir . "/avatar";
	my $dir;
	my $oid = $object->get_oid;
	my $path;
	my $digest;
	my $buffer;

    if( ! -d $datadir )
    {
        return -1;
    }
    else
    {
		$digest = md5_hex( $oid );
		$dir = $datadir . "/" . substr( $digest , 0 , 2 ) . "/" . substr( $digest , 2 , 2 ) . "/" .substr( $digest , 4 , 2 );
		$path = $dir . "/" .$oid;

#		$path .= ".".$type if( defined $type );

		Vhffs::Functions::create_dir( $dir );
		open FORIG , "$file" or return -2;
		open FCOPY , ">$path" or return -3;

		binmode( FCOPY );
		while( read( FORIG , $buffer , 1024 ) )
		{
			print FCOPY $buffer;
		}

		close( FCOPY ) or return( -4 );
		close( FORIG ) or return( -4 );

		
    }
	return 1;
}

sub exists_avatar
{
	my $vhffs = shift;
	my $object = shift;

	return undef unless defined $object;

	my $digest = md5_hex( $object->get_oid );
	my $path = $vhffs->get_config->get_datadir.'/avatar/'.substr( $digest , 0 , 2 ).'/'.substr( $digest , 2 , 2 ).'/'.substr( $digest , 4 , 2 ).'/'.$object->get_oid;;

	return undef unless -f $path;
	return $path;
}

1;


__END__

=head1 NAME

Vhffs::Panel::Avatar - upload and get avatar for VHFFS

=head1 SYNOPSIS

	use Vhffs::Main;
	use Vhffs::Panel::Avatar;

	my $vhffs = init Vhffs::Main;
	
	....
	(considers that you create or handle an object in $object var)
	....
	Vhffs::Panel::Avatar::store_avatar( $vhffs , $object , $filename );

	my $path = Vhffs::Panel::Avatar::exists_avatar( $vhffs , $object );
	if( ! defined ( $path ) ) )
	{
		print "No avatar for this object" );
	}
	else
	{
		print "The avatar for this object is stored in the file " . $path ;
	}

=head1 AUTHOR

	Julien Delange <julien AT gunnm DOT org>

=head1 COPYRIGHT

	Copyright 2006 Julien Delange
