Difference between revisions of "API:EPrints/DataObj/File"
Line 8: | Line 8: | ||
'''EPrints::DataObj::File''' - a stored file | '''EPrints::DataObj::File''' - a stored file | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | |||
<!-- Pod2Wiki=head_description --> | <!-- Pod2Wiki=head_description --> | ||
==DESCRIPTION== | ==DESCRIPTION== | ||
Line 23: | Line 20: | ||
This class is a subclass of [[API:EPrints/DataObj/SubObject|EPrints::DataObj::SubObject]]. | This class is a subclass of [[API:EPrints/DataObj/SubObject|EPrints::DataObj::SubObject]]. | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | |||
<!-- Pod2Wiki=head_core_fields --> | <!-- Pod2Wiki=head_core_fields --> | ||
==CORE FIELDS== | ==CORE FIELDS== | ||
− | + | * fileid | |
− | + | : Unique identifier for this file. | |
− | |||
+ | * rev_number (int) | ||
+ | : The number of the current revision of this file. | ||
− | + | * datasetid | |
− | + | : Id of the dataset of the parent object. | |
− | |||
− | |||
− | + | * objectid | |
+ | : Id of the parent object. | ||
− | + | * filename | |
− | + | : Name of the file (may contain directory separators). | |
− | |||
+ | * mime_type | ||
+ | : MIME type of the file (e.g. "image/png"). | ||
− | + | * hash | |
− | + | : Check sum of the file. | |
− | |||
− | |||
− | + | * hash_type | |
+ | : Name of check sum algorithm used (e.g. "MD5"). | ||
− | + | * filesize | |
− | + | : Size of the file in bytes. | |
− | |||
+ | * mtime | ||
+ | : Last modification time of the file. | ||
− | + | * url | |
− | + | : Virtual field for storing the file's URL. | |
− | |||
− | |||
− | + | * data | |
+ | : Virtual field for storing the file's content. | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<!-- Pod2Wiki=head_methods --> | <!-- Pod2Wiki=head_methods --> | ||
==METHODS== | ==METHODS== | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<!-- Pod2Wiki=head_constructor_methods --> | <!-- Pod2Wiki=head_constructor_methods --> | ||
===Constructor Methods=== | ===Constructor Methods=== | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_new_from_filename --> | |
− | <!-- Pod2Wiki= | + | ===new_from_filename=== |
− | |||
− | + | <source lang="perl">$dataobj = EPrints::DataObj::File->new_from_filename( $session, $dataobj, $filename ) | |
+ | |||
+ | </source> | ||
Convenience method to get an existing File object for $filename stored in $dataobj. | Convenience method to get an existing File object for $filename stored in $dataobj. | ||
Returns undef if no such record exists. | Returns undef if no such record exists. | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_create_from_data --> | |
− | <!-- Pod2Wiki= | + | ===create_from_data=== |
− | |||
− | + | <source lang="perl">$dataobj = EPrints::DataObj::File->create_from_data( $session, $data [, $dataset ] ) | |
+ | |||
+ | </source> | ||
Create a new File record using $data. | Create a new File record using $data. | ||
Private data elements: | Private data elements: | ||
− | + | <pre> _content - content to pass to [[API:EPrints/DataObj/File#set_file|set_file]]. | |
− | _filepath - path to source file used for improved mime-type detection | + | _filepath - path to source file used for improved mime-type detection</pre> |
− | + | ||
− | < | ||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | |||
<!-- Pod2Wiki=head_class_methods --> | <!-- Pod2Wiki=head_class_methods --> | ||
===Class Methods=== | ===Class Methods=== | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_get_system_field_info --> | |
− | <!-- Pod2Wiki= | + | ===get_system_field_info=== |
− | ==== | + | |
+ | <source lang="perl">$thing = EPrints::DataObj::File->get_system_field_info | ||
− | + | </source> | |
Core fields. | Core fields. | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_get_dataset_id --> | |
− | <!-- Pod2Wiki= | + | ===get_dataset_id=== |
− | ==== | + | |
+ | <source lang="perl">$dataset = EPrints::DataObj::File->get_dataset_id | ||
− | + | </source> | |
Returns the id of the [[API:EPrints/DataSet|EPrints::DataSet]] object to which this record belongs. | Returns the id of the [[API:EPrints/DataSet|EPrints::DataSet]] object to which this record belongs. | ||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | |||
<!-- Pod2Wiki=head_object_methods --> | <!-- Pod2Wiki=head_object_methods --> | ||
===Object Methods=== | ===Object Methods=== | ||
− | + | * $new_file = $file->clone( $parent ) | |
− | + | : Clone the $file object (including contained files) and return the new object. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | Clone the $file object (including contained files) and return the new object. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | * $success = $file->remove | |
− | Delete the stored file. | + | : Delete the stored file. |
− | + | * $file->update( $epdata ) | |
− | + | * $filename = $file->get_local_copy() | |
− | + | : Return the name of a local copy of the file (may be a {{API:PodLink|file=File/Temp|package_name=File::Temp|section=|text=File::Temp}} object). | |
+ | : Will retrieve and cache the remote object if necessary. | ||
− | + | * $success = $file->add_file( $filepath, $filename [, $preserve_path ] ) | |
− | + | : Read and store the contents of $filepath at $filename. | |
− | |||
− | |||
− | + | : If $preserve_path is untrue will strip any leading path in $filename. | |
− | |||
− | + | * $bytes = $file->upload( $filehandle, $filename, $filesize [, $preserve_path ] ) | |
+ | : Read and store the data from $filehandle at $filename at the next revision number. | ||
− | + | : If $preserve_path is untrue will strip any leading path in $filename. | |
− | |||
− | |||
+ | : Returns the number of bytes read from $filehandle or undef on failure. | ||
− | + | * $success = $stored->write_copy( $filename ) | |
− | + | : Write a copy of this file to $filename. | |
− | |||
− | |||
− | + | : Returns true if the written file contains the same number of bytes as the stored file. | |
− | |||
− | + | * $success = $stored->write_copy_fh( $filehandle ) | |
+ | : Write a copy of this file to $filehandle. | ||
− | + | * $md5 = $stored->generate_md5 | |
− | + | : Calculates and returns the MD5 for this file. | |
− | |||
+ | * $digest = $file->generate_sha( [ ALGORITHM ] ) | ||
+ | : Generate a SHA for this file, see {{API:PodLink|file=Digest/SHA|package_name=Digest::SHA|section=|text=Digest::SHA}} for a list of supported algorithms. Defaults to "256" (SHA-256). | ||
− | + | : Returns the hex-encoded digest. | |
− | |||
− | |||
− | |||
− | + | * $stored->add_plugin_copy( $plugin, $sourceid ) | |
− | + | : Add a copy of this file stored using $plugin identified by $sourceid. | |
− | + | * $stored->remove_plugin_copy( $plugin ) | |
+ | : Remove the copy of this file stored using $plugin. | ||
− | + | * $success = $stored->get_file( CALLBACK [, $offset, $n ] ) | |
+ | : Get the contents of the stored file. | ||
− | + | : $offset is the position in bytes to start reading from, defaults to 0. | |
− | |||
− | |||
+ | : $n is the number of bytes to read, defaults to <code>filesize</code>. | ||
− | + | : CALLBACK is: | |
− | |||
− | |||
− | |||
− | $ | + | <pre> sub { |
− | + | my( $buffer ) = @_; | |
+ | ... | ||
+ | return 1; | ||
+ | }</pre> | ||
− | + | * $content_length = $stored->set_file( CONTENT, $content_length ) | |
+ | : Write $content_length bytes from CONTENT to the file object. Updates <code>filesize</code> and <code>hash</code> (you must call [[API:EPrints/DataObj/File#commit|commit]]). | ||
− | + | : Returns $content_length or undef on failure. | |
− | |||
− | |||
+ | : CONTENT may be one of: | ||
− | + | ** CODEREF | |
− | + | :: Will be called until it returns empty string (""). | |
− | |||
− | |||
− | + | ** SCALARREF | |
− | + | :: A scalar reference to a string of octets that will be written as-is. | |
− | + | ** GLOB | |
− | + | :: Will be treated as a file handle and read with sysread(). | |
− | |||
+ | * $content_length = $file->set_file_chunk( CONTENT, $content_length, $offset, $total ) | ||
+ | : Write a chunk of data to the content, overwriting or appending to the existing content. See [[API:EPrints/DataObj/File#set_file|set_file]] for CONTENT and $content_length. <code>filesize</code> is updated if $offset + $content_length is greater than the current <code>filesize</code>. | ||
− | + | : $offset is the starting point (in bytes) to write. $total is the total file size, used to determine where to store the content. | |
− | |||
− | |||
− | |||
− | + | : Returns the number of bytes written or undef on failure. | |
− | |||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_see_also --> | |
− | <!-- Pod2Wiki= | + | ==SEE ALSO== |
− | == | + | : [[API:EPrints/DataObj|EPrints::DataObj]] and [[API:EPrints/DataSet|EPrints::DataSet]]. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=head_copyright --> | |
− | + | ==COPYRIGHT== | |
− | + | : Copyright 2000-2011 University of Southampton. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | <!-- Pod2Wiki= | ||
− | == | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | : This file is part of EPrints http://www.eprints.org/. | |
− | |||
− | |||
− | |||
− | |||
− | + | : EPrints is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | |
− | |||
− | |||
+ | : EPrints 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 Lesser General Public License for more details. | ||
− | + | : You should have received a copy of the GNU Lesser General Public License along with EPrints. If not, see http://www.gnu.org/licenses/. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
<!-- Pod2Wiki= --> | <!-- Pod2Wiki= --> | ||
− | + | <!-- Pod2Wiki=_postamble_ --> | |
− | <!-- Pod2Wiki= | ||
− | |||
− | |||
− | |||
<!-- Edit below this comment --> | <!-- Edit below this comment --> | ||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 09:57, 22 January 2013
EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects
Latest Source Code (3.4, 3.3) | Revision Log | Before editing this page please read Pod2Wiki
Contents
NAME
EPrints::DataObj::File - a stored file
DESCRIPTION
This class contains the technical metadata associated with a file. A file is a sequence of bytes stored in the storage layer (a "stored object"). Utility methods for storing and retrieving the stored object from the storage layer are made available.
Revision numbers on File work slightly differently to other objects. A File is only revised when it's stored object is changed and not when changes to it's metadata are made.
This class is a subclass of EPrints::DataObj::SubObject.
CORE FIELDS
- fileid
- Unique identifier for this file.
- rev_number (int)
- The number of the current revision of this file.
- datasetid
- Id of the dataset of the parent object.
- objectid
- Id of the parent object.
- filename
- Name of the file (may contain directory separators).
- mime_type
- MIME type of the file (e.g. "image/png").
- hash
- Check sum of the file.
- hash_type
- Name of check sum algorithm used (e.g. "MD5").
- filesize
- Size of the file in bytes.
- mtime
- Last modification time of the file.
- url
- Virtual field for storing the file's URL.
- data
- Virtual field for storing the file's content.
METHODS
Constructor Methods
new_from_filename
$dataobj = EPrints::DataObj::File->new_from_filename( $session, $dataobj, $filename )
Convenience method to get an existing File object for $filename stored in $dataobj.
Returns undef if no such record exists.
create_from_data
$dataobj = EPrints::DataObj::File->create_from_data( $session, $data [, $dataset ] )
Create a new File record using $data.
Private data elements:
_content - content to pass to [[API:EPrints/DataObj/File#set_file|set_file]]. _filepath - path to source file used for improved mime-type detection
Class Methods
get_system_field_info
$thing = EPrints::DataObj::File->get_system_field_info
Core fields.
get_dataset_id
$dataset = EPrints::DataObj::File->get_dataset_id
Returns the id of the EPrints::DataSet object to which this record belongs.
Object Methods
- $new_file = $file->clone( $parent )
- Clone the $file object (including contained files) and return the new object.
- $success = $file->remove
- Delete the stored file.
- $file->update( $epdata )
- $filename = $file->get_local_copy()
- Return the name of a local copy of the file (may be a File::Temp object).
- Will retrieve and cache the remote object if necessary.
- $success = $file->add_file( $filepath, $filename [, $preserve_path ] )
- Read and store the contents of $filepath at $filename.
- If $preserve_path is untrue will strip any leading path in $filename.
- $bytes = $file->upload( $filehandle, $filename, $filesize [, $preserve_path ] )
- Read and store the data from $filehandle at $filename at the next revision number.
- If $preserve_path is untrue will strip any leading path in $filename.
- Returns the number of bytes read from $filehandle or undef on failure.
- $success = $stored->write_copy( $filename )
- Write a copy of this file to $filename.
- Returns true if the written file contains the same number of bytes as the stored file.
- $success = $stored->write_copy_fh( $filehandle )
- Write a copy of this file to $filehandle.
- $md5 = $stored->generate_md5
- Calculates and returns the MD5 for this file.
- $digest = $file->generate_sha( [ ALGORITHM ] )
- Generate a SHA for this file, see Digest::SHA for a list of supported algorithms. Defaults to "256" (SHA-256).
- Returns the hex-encoded digest.
- $stored->add_plugin_copy( $plugin, $sourceid )
- Add a copy of this file stored using $plugin identified by $sourceid.
- $stored->remove_plugin_copy( $plugin )
- Remove the copy of this file stored using $plugin.
- $success = $stored->get_file( CALLBACK [, $offset, $n ] )
- Get the contents of the stored file.
- $offset is the position in bytes to start reading from, defaults to 0.
- $n is the number of bytes to read, defaults to
filesize
.
- CALLBACK is:
sub { my( $buffer ) = @_; ... return 1; }
- $content_length = $stored->set_file( CONTENT, $content_length )
- Write $content_length bytes from CONTENT to the file object. Updates
filesize
andhash
(you must call commit).
- Returns $content_length or undef on failure.
- CONTENT may be one of:
- CODEREF
- Will be called until it returns empty string ("").
- SCALARREF
- A scalar reference to a string of octets that will be written as-is.
- GLOB
- Will be treated as a file handle and read with sysread().
- $content_length = $file->set_file_chunk( CONTENT, $content_length, $offset, $total )
- Write a chunk of data to the content, overwriting or appending to the existing content. See set_file for CONTENT and $content_length.
filesize
is updated if $offset + $content_length is greater than the currentfilesize
.
- $offset is the starting point (in bytes) to write. $total is the total file size, used to determine where to store the content.
- Returns the number of bytes written or undef on failure.
SEE ALSO
COPYRIGHT
- Copyright 2000-2011 University of Southampton.
- This file is part of EPrints http://www.eprints.org/.
- EPrints is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- EPrints 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 Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License along with EPrints. If not, see http://www.gnu.org/licenses/.