<commit_before>package pman.bg.tasks;

import tannus.io.*;
import tannus.ds.*;
import tannus.sys.*;
import tannus.async.*;
import tannus.async.promises.*;
import tannus.sys.Path;
import tannus.http.Url;
import tannus.sys.FileSystem as Fs;

import ffmpeg.Fluent;

import pman.bg.Dirs;
import pman.bg.db.*;
import pman.bg.media.*;
import pman.bg.media.Media;
import pman.bg.media.MediaData;
import pman.bg.media.MediaRow;

import Slambda.fn;
import edis.Globals.*;

using StringTools;
using tannus.ds.StringUtils;
using Slambda;
using tannus.ds.ArrayTools;
using tannus.FunctionTools;
using tannus.async.Asyncs;
using pman.bg.MediaTools;

class LoadMediaData extends Task2<MediaData> {
    /* Constructor Function */
    public function new(media:Media, db:Database):Void {
        super();

        this.media = media;
        this.db = db;
        this.data = new MediaData();
    }

/* === Instance Methods === */

    /**
      * execute [this]
      */
    override function execute(done: Cb<MediaData>):Void {
        [tryto_load, fill_missing_info].series(function(?error) {
            if (error != null) {
                done(error, null);
            }
            else {
                done(null, data);
            }
        });
    }

    private function tryto_load(done: VoidCb):Void {
        switch ( media.source ) {
            case MediaSource.MSLocalPath( path ):
                if (!Fs.exists( path )) {
                    done('Error: File "$path" does not exist');
                }
                else {
                    attempt_load( done );
                }

            default:
                attempt_load( done );
        }
    }

    private function attempt_load(done: VoidCb):Void {
        if (media.id != null) {
            store.getRowById(media.id, function(?error, ?irow) {
                if (error != null)
                    return done(error);

                data = new MediaData();
                data.pullRow(untyped irow, done);
            });
        }
        else {
            var uri:String = media.source.toUri();
            store.getRowByUri(uri, function(?error:Dynamic, ?row:MediaRow) {
                if (error != null) {
                    return done( error );
                }
                else {
                    if (row == null) {
                        return create_new( done );
                    }
                    else {
                        media.applyRow(row, done);
                    }
                }
            });
        }
    }

    /**
      *
      */
    private function create_new(done: VoidCb):Void {
        data = new MediaData();
        loadMediaMetadata(function(?error, ?meta) {
            if (error != null) {
                return done( error );
            }
            else {
                data.meta = meta;
                push_data_to_db( done );
            }
        });
    }

    /**
      * push new data to the database
      */
    private function push_data_to_db(done: VoidCb):Void {
        var raw = data.toRow();
        var mraw:MediaRow = {
            _id: media.id,
            uri: media.source.toUri(),
            data: raw
        };

        store.insertRow(mraw, function(?error, ?row) {
            if (error != null) {
                return done( error );
            }
            else {
                media.applyRow(row, done);
            }
        });
    }

    /**
      * attempt to fill in missing info and stuff
      */
    private function fill_missing_info(done : VoidCb):Void {
        if (data.meta == null || data.meta.isIncomplete()) {
            loadMediaMetadata(function(?error:Dynamic, ?meta) {
                if (error != null) {
                    return done( error );
                }
                else {
                    data.meta = meta;
                    done();
                }
            });
        }
        else {
            done();
        }
    }

    /**
      * load the Media's metadata
      */
    private function loadMediaMetadata(done: Cb<MediaMetadata>):Void {
        switch ( media.source ) {
            case MediaSource.MSLocalPath(path):
                LoadMediaMetadata.load( path ).toAsync( done );

            default:
                done('Betty', null);
        }
    }

/* === Computed Instance Fields === */

    private var store(get, never):MediaTable;
    private inline function get_store() return db.media;

/* === Instance Fields === */

    public var media: Media;
    public var db: Database;
    public var data: Null<MediaData>;
}<commit_msg>factored the 'media' object out of pman.bg.tasks.LoadMediaData<commit_after>package pman.bg.tasks;

import tannus.io.*;
import tannus.ds.*;
import tannus.sys.*;
import tannus.async.*;
import tannus.async.promises.*;
import tannus.sys.Path;
import tannus.http.Url;
import tannus.sys.FileSystem as Fs;

import ffmpeg.Fluent;

import pman.bg.Dirs;
import pman.bg.db.*;
import pman.bg.media.*;
import pman.bg.media.Media;
import pman.bg.media.MediaData;
import pman.bg.media.MediaRow;

import Slambda.fn;
import edis.Globals.*;

using StringTools;
using tannus.ds.StringUtils;
using Slambda;
using tannus.ds.ArrayTools;
using tannus.FunctionTools;
using tannus.async.Asyncs;
using pman.bg.MediaTools;

class LoadMediaData extends Task2<MediaData> {
    /* Constructor Function */
    public function new(uri:String, db:Database):Void {
        super();

        //this.media = media;
        this.mediaUri = uri.toUri();
        this.mediaSource = this.mediaUri.toMediaSource();
        this.db = db;
        this.data = new MediaData();
    }

/* === Instance Methods === */

    /**
      * execute [this]
      */
    override function execute(done: Cb<MediaData>):Void {
        trace('Starting LoadMediaData');
        done = done.wrap(function(dun, ?error, ?data) {
            if (error != null)
                trace('LoadMediaData has failed');
            else
                trace('LoadMediaData completed successfully');
            dun(error, data);
        });

        [tryto_load, fill_missing_info].series(function(?error) {
            if (error != null) {
                done(error, null);
            }
            else {
                done(null, data);
            }
        });
    }

    private function tryto_load(done: VoidCb):Void {
        switch ( mediaSource ) {
            case MediaSource.MSLocalPath( path ):
                if (!Fs.exists( path )) {
                    done('Error: File "$path" does not exist');
                }
                else {
                    attempt_load( done );
                }

            default:
                attempt_load( done );
        }
    }

    /**
      * attempt to load the MediaRow
      */
    private function attempt_load(done: VoidCb):Void {
        trace('attempting to load MediaRow document');
        store.cogRow(mediaUri.toUri(), function(?error:Dynamic, ?row:MediaRow) {
            if (error != null) {
                return done( error );
            }
            else {
                if (row == null) {
                    return create_new( done );
                }
                else {
                    trace('LOADED: MediaRow document');
                    trace( row );
                    data.pullMediaRow(row, done);
                }
            }
        });
    }

    /**
      * 
      */
    private function create_new(done: VoidCb):Void {
        data = new MediaData();
        loadMediaMetadata(function(?error, ?meta) {
            if (error != null) {
                return done( error );
            }
            else {
                data.meta = meta;
                push_data_to_db( done );
            }
        });
    }

    /**
      * push new data to the database
      */
    private function push_data_to_db(done: VoidCb):Void {
        var raw = data.toRow();
        var mraw:MediaRow = {
            uri: mediaUri,
            data: raw
        };

        store.insertRow(mraw, function(?error, ?row) {
            if (error != null) {
                return done( error );
            }
            else {
                data.pullMediaRow(row, done);
            }
        });
    }

    /**
      * attempt to fill in missing info and stuff
      */
    private function fill_missing_info(done : VoidCb):Void {
        if (data.meta == null || data.meta.isIncomplete()) {
            loadMediaMetadata(function(?error:Dynamic, ?meta) {
                if (error != null) {
                    return done( error );
                }
                else {
                    data.meta = meta;
                    done();
                }
            });
        }
        else {
            done();
        }
    }

    /**
      * load the Media's metadata
      */
    private function loadMediaMetadata(done: Cb<MediaMetadata>):Void {
        switch ( mediaSource ) {
            case MediaSource.MSLocalPath(path):
                LoadMediaMetadata.load( path ).toAsync( done );

            default:
                done('Betty', null);
        }
    }

/* === Computed Instance Fields === */

    private var store(get, never):MediaTable;
    private inline function get_store() return db.media;

/* === Instance Fields === */

    //public var media: Media;
    public var mediaUri: String;
    public var mediaSource: MediaSource;
    public var db: Database;
    public var data: Null<MediaData>;
}
{'fraction_non_alphanumeric': 0.11713046238493281, 'fraction_numerical': 0.00021161781822029416, 'mean_word_length': 1.4646675358539765, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2629122', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2615, 'n_words': 598}