<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Represents a single image
using System.Windows.Markup;

namespace ImageTag
{
    public class ImageFile : BaseIObservable
    {
        private string _previewURL;
        private bool _isSelected = false;
        private string _dimensions;
        private List<string> _tagList;

        public ImageFile(string filePath)
        {
            PreviewURL = filePath;
            Dimensions = "<TBD>";
            _tagList = MakeTags();
        }

        public string PreviewURL
        {
            get { return _previewURL; }
            set
            {
                _previewURL = value;
                RaisePropertyChanged("PreviewURL");
            }
        }
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }
        public string Dimensions
        {
            get { return _dimensions; }
            set
            {
                _dimensions = value;
                RaisePropertyChanged("Dimensions");
            }
        }

        private static char[] tagSplit = new char[] { '+' };
        private List<string> MakeTags()
        {
            var fileN = Path.GetFileNameWithoutExtension(_previewURL);
            var bits = fileN.Split(tagSplit);
            var tagList = new List<string>();
            for (int i = 1; i < bits.Length; i++)
                tagList.Add(bits[i]);
            return tagList;
        }

        public bool HasTag(string tag)
        {
            return _tagList.Contains(tag);
        }

        public List<string> Tags()
        {
            return _tagList;
        }

        public void RemoveTags(IList tagsToRemove)
        {
            foreach (var tag in tagsToRemove)
            {
                _tagList.Remove(tag as string);
            }

            // TODO actually change the filename!
        }

        public void AddTag(string tag)
        {
            if (!_tagList.Contains(tag))
                _tagList.Add(tag);

            // TODO actually change the filename!
        }

        public void ChangeTag(string oldtag, string newtag)
        {
            if (_tagList.Contains(oldtag))
                _tagList.Remove(oldtag);
            if (!_tagList.Contains(newtag))
                _tagList.Add(newtag);

            // TODO actually change the filename!
        }

    }
}
<commit_msg>Actually rename files on tag changes<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows;

// Represents a single image

// TODO tag character as an option
// TODO previewURL -> a preview thumbnail image. Need to store original path separately.

namespace ImageTag
{
    public class ImageFile : BaseIObservable
    {
        private string _previewURL;
        private bool _isSelected = false;
        private string _dimensions;
        private List<string> _tagList;

        private string _baseFile;

        public ImageFile(string filePath)
        {
            PreviewURL = filePath;
            Dimensions = "<TBD>";
            _tagList = MakeTags();
        }

        public string PreviewURL 
        {
            get { return _previewURL; }
            set
            {
                _previewURL = value;
                RaisePropertyChanged("PreviewURL");
            }
        }
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }
        public string Dimensions
        {
            get { return _dimensions; }
            set
            {
                _dimensions = value;
                RaisePropertyChanged("Dimensions");
            }
        }

        private static char[] tagSplit = { '+' };
        private List<string> MakeTags()
        {
            var fileN = Path.GetFileNameWithoutExtension(_previewURL);
            var bits = fileN.Split(tagSplit);

            _baseFile = bits[0];

            var tagList = new List<string>();
            for (int i = 1; i < bits.Length; i++)
                tagList.Add(bits[i]);
            return tagList;
        }

        public bool HasTag(string tag)
        {
            return _tagList.Contains(tag);
        }

        public List<string> Tags()
        {
            return _tagList;
        }

        public void RemoveTags(IList tagsToRemove)
        {
            bool anyHits = false;
            foreach (var tag in tagsToRemove)
            {
                anyHits |= _tagList.Remove(tag as string);
            }

            if (anyHits)
                RenameFile();
        }

        public void AddTag(string tag)
        {
            if (!_tagList.Contains(tag))
            {
                _tagList.Add(tag);
                RenameFile();
            }
        }

        public void ChangeTag(string oldtag, string newtag)
        {
            bool anyChange = false;
            if (_tagList.Contains(oldtag))
            {
                _tagList.Remove(oldtag);
                anyChange = true;
            }
            if (!_tagList.Contains(newtag))
            {
                _tagList.Add(newtag);
                anyChange = true;
            }

            if (anyChange)
                RenameFile();
        }

        private void RenameFile()
        {
            string filename = _baseFile;
            foreach (var aTag in _tagList)
            {
                filename += "+" + aTag;
            }

            string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            foreach (char c in invalid)
            {
                filename = filename.Replace(c.ToString(), "");
            }

            string dest = Path.Combine(Path.GetDirectoryName(_previewURL),filename) + Path.GetExtension(_previewURL);

            // TODO check if dest length > 256

            File.Move(_previewURL, dest);
            _previewURL = dest;
        }

    }
}

{'fraction_non_alphanumeric': 0.07866686683681129, 'fraction_numerical': 0.0009007656508031827, 'mean_word_length': 1.1907267346267676, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7337232', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1598, 'n_words': 389}