Gossamer Forum
Quote Reply
Tempfile -- Tempdir
Alex,

Would it be possible to generalize the Tempfile.pm to handle an entire directory? Such that when the temp directory object went out of scope, it was deleted, recursively, if necessary (rm -R $tempdir)

What I am trying to do, or am doing, but not in an OOP way, is using $tempdir/$id to hold the current uploads. This mimics the $attach_dir/$id path that the successful upload and process will end up in. (File::Copy($tempfile, $attachfile))

Once done, the $tempdir/$id directory can go away. Housekeeping.

I can do this manually, of course, but this is a great expansion of the $tempfile object, to handling "work directories" that get cleaned up automatically.

I'm tryin to understand the module hierarchy, starting with Base, and using that to develop both the multiple file upload program (move it over to the OOP format) and the Image Gallery program.

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Tempfile -- Tempdir In reply to
Hi,

That would definately be a useful addition. I don't really like how TempFile is implemented right now (dereferencing it to access it), and would do it differently if I were to do it over again.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Tempfile -- Tempdir In reply to
Any suggestions how to do this ? :)

I figure that is a small enough effort to try on, and I do need to use temp files and temp directories a lot during this programs upload phase -- for identification and for processing thumnails, and maybe eventually zipping them up.

I haven't figured out the BASe class yet, which I think is some of the problems. I'm not sure how the various parts are called, and when, and I think I almost just figured out how the parameters are passed.

But there are parts like $self->can(init) and I can't find that, or follow that through.

It's almost like an alien world :)

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: Tempfile -- Tempdir In reply to
Hmm, something like:

Code:
package GT::TempDir;
use strict;
use vars qw/@ISA $ATTRIBS $TMP_DIR/;
use GT::Base;
use GT::MD5 qw/md5_hex/;
@ISA = qw/GT::Base/;
$ATTRIBS = { dir => undef, files => [] };

sub find_dir {
# Copy from GT::TempFile; Basically just called once
# to find a system tmp directory that is writeable.
}

sub init {
# Called whenever a new object is created.
my $self = shift;
$TMP_DIR ||= find_dir();
$self->{dir} = $TMP_DIR . '/' . md5_hex( time() . $$ . rand() );
mkdir ($self->{dir}, 0700) or return $self->error ('MKDIR', 'WARN', "$!");
return $self;
}

sub new_file {
# Create a new tmp file in the directory.
my $self = shift;
my $file = $self->{dir} . '/' . md5_hex ( time() . $$ . rand() );
push @{$self->{files}}, $file;
return $file;
}

sub DESTROY {
# Object is gone, let's clean up.
my $self = shift;
foreach my $file (@{$self->{files}}) {
unlink $file;
}
rmdir $self->{dir};
}
This isn't tested, but should give you an idea. Basically how you would use this is:

Code:
{
my $tmp_dir = new GT::TempDir or die "Can't find writeable directory: $GT::TempDir::error";
my $tmp_file = $tmp_dir->new_file;
open (FILE, "> $tmp_file") or ..;
...
close FILE;
}
# tmp_dir is now out of scope, so it will get cleaned up.
Let me know what you think!

Cheers,

Alex

--
Gossamer Threads Inc.