ONO::Lib::File

package ONO::Lib::File;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# 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.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::IO;

use ONO::Lib::File::Convert;
use ONO::Lib::File::Server;
use ONO::Lib::File::System;

use ONO::Lib::Image::Magick;
use ONO::Lib::Video::ToolBox;
use ONO::Lib::Audio::ToolBox;

#: This library contains everything related to file management

sub convert {

#: Convert various file types. Some types are converted directly, others
#: will be added to the CronSync media processing pipeline.
#:
#: This also supports PDF/HEIC to JPG conversion, M4A/WMV to MP3, various
#: video to MP4 conversions, as well as XLS to CSV.

my ($self,$file1,$file2) = @_;

$file1 = ONO::IO->cleanup($file1);
$file2 = ONO::IO->cleanup($file2);

my $report;

if ($file1 && ONO::IO->exists($file1) && (!$file2 || !ONO::IO->exists($file2))) {

$report = "error: no logic for $file1, $file2";

# text

if ($file1 =~ /\.txt$/i) {

# txt to html

if ($file2 =~ /\.html$/i) {

my $TXT = ONO::IO->load($file1);
$TXT =~ s~(\n|\r|\t)~<br>~g;
ONO::IO->store($file2,qq~<html><head></head><body>$TXT</body></html>~);

$report = "convert: $file1, $file2";

}

}

# pdf / heic

if ($file1 =~ /\.(pdf|heic)$/i) {

# pdf/heic to jpg

if ($file2 =~ /\.jpg$/i) {

my $return = ONO::Lib::Image::Magick->convert("/$file1","/$file2","d");

$report = "convert: $file1, $file2 ($return)";

}

}

# audio

if ($file1 =~ /\.(m4a|wma)$/i) {

# video to mp4 - no target filename required

if (!$file2 || $file2 =~ /\.mp3$/i) {

ONO::Lib::Audio::ToolBox->convertticket($file1);

$report = "convert: $file1, $file2";

}

}

# video

if ($file1 =~ /\.(mpg|mpeg|m2ts|mov|mkv|avi|vob|wmv|flv|f4v|divx|webm|mp4c|ts)$/i) {

# video to mp4 - no target filename required

if (!$file2 || $file2 =~ /\.mp4$/i) {

ONO::Lib::Video::ToolBox->convertticket($file1);

$report = "convert: $file1, $file2";

}

}

# excel

if ($file1 =~ /\.(xls|xlsx)$/i) {

# excel to csv

if ($file2 =~ /\.csv$/i) {

my $return = ONO::Lib::File::Convert->excel_csv($file1,$file1);

$report = "convert: $file1, $file2 ($return)";

}

}

} else {

$report = "error: no such file, or target exists",

}

return $report;

}

sub convertfilename {

#: Generate a filename for the converted target file, usually the original
#: filename followed by a "_converted" string.

my ($self,$file,$new) = @_;

$file = ONO::IO->cleanup($file);
$new = ONO::IO->cleanup($new);

my ($file,$ext) = ONO::IO->getfileext($file);

return "${file}_converted.$new";

}

###############################################################################
# end of script
###############################################################################

1;

__END__