Refactoring and making default name better

This commit is contained in:
Lars Brubaker 2022-01-24 14:49:29 -08:00
parent 22e0cd375b
commit 2394e742a3
20 changed files with 129 additions and 109 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, John Lewin
Copyright (c) 2022, John Lewin, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -269,9 +269,9 @@ namespace MatterHackers.MatterControl.Library
foreach (var item in items)
{
if (item is FileSystemItem fileItem
&& File.Exists(fileItem.Path))
&& File.Exists(fileItem.FilePath))
{
File.Delete(fileItem.Path);
File.Delete(fileItem.FilePath);
}
}
@ -348,7 +348,7 @@ namespace MatterHackers.MatterControl.Library
public Task<ILibraryContainer> GetContainer(Action<double, string> reportProgress)
{
return Task.FromResult<ILibraryContainer>(
new FileSystemContainer(this.Path)
new FileSystemContainer(this.FilePath)
{
UseIncrementedNameDuringTypeChange = this.UseIncrementedNameDuringTypeChange,
Name = this.Name

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, John Lewin
Copyright (c) 2022, John Lewin, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -45,11 +45,11 @@ namespace MatterHackers.MatterControl.Library
}
}
public string AssetPath => this.Path;
public string AssetPath => this.FilePath;
public string ContentType => System.IO.Path.GetExtension(this.Path).ToLower().Trim('.');
public string ContentType => Path.GetExtension(this.FilePath).ToLower().Trim('.');
public string FileName => System.IO.Path.GetFileName(this.Path);
public string FileName => Path.GetFileName(this.FilePath);
/// <summary>
/// Gets the size, in bytes, of the current file.
@ -58,12 +58,12 @@ namespace MatterHackers.MatterControl.Library
public Task<StreamAndLength> GetStream(Action<double, string> reportProgress)
{
if (File.Exists(this.Path)
&& (ApplicationController.Instance.IsLoadableFile(this.Path)
|| (System.IO.Path.GetExtension(this.Path) is string extension
if (File.Exists(this.FilePath)
&& (ApplicationController.Instance.IsLoadableFile(this.FilePath)
|| (Path.GetExtension(this.FilePath) is string extension
&& string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase))))
{
var stream = File.OpenRead(this.Path);
var stream = File.OpenRead(this.FilePath);
return Task.FromResult(new StreamAndLength()
{
Stream = stream,

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, John Lewin
Copyright (c) 2022, John Lewin, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -38,23 +38,23 @@ namespace MatterHackers.MatterControl.Library
/// </summary>
public class FileSystemItem : ILibraryItem
{
public FileSystemItem(string path)
public FileSystemItem(string filePath)
{
this.Path = path;
this.FilePath = filePath;
var type = GetType();
try
{
if (type == typeof(FileSystemFileItem))
{
var fileInfo = new FileInfo(path);
var fileInfo = new FileInfo(filePath);
this.DateCreated = fileInfo.CreationTime;
this.DateModified = fileInfo.LastWriteTime;
}
else
{
var directoryInfo = new DirectoryInfo(path);
var directoryInfo = new DirectoryInfo(filePath);
this.DateCreated = directoryInfo.CreationTime;
this.DateModified = directoryInfo.LastWriteTime;
@ -73,7 +73,7 @@ namespace MatterHackers.MatterControl.Library
public DateTime DateModified { get; }
public virtual string ID => agg_basics.GetLongHashCode(this.Path).ToString();
public virtual string ID => agg_basics.GetLongHashCode(this.FilePath).ToString();
public virtual bool IsProtected => false;
@ -87,31 +87,45 @@ namespace MatterHackers.MatterControl.Library
{
get
{
return System.IO.Path.GetFileName(this.Path);
return System.IO.Path.GetFileNameWithoutExtension(this.FilePath);
}
set
{
if (Name != value)
{
string sourceFile = this.Path;
string sourceFile = this.FilePath;
if (File.Exists(sourceFile))
{
string extension = System.IO.Path.GetExtension(sourceFile);
string destFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(sourceFile), value);
destFile = System.IO.Path.ChangeExtension(destFile, extension);
string extension = Path.GetExtension(sourceFile);
string destFile = Path.Combine(Path.GetDirectoryName(sourceFile), value);
destFile = Path.ChangeExtension(destFile, extension);
var uniqueFileIncrement = 0;
while(File.Exists(destFile))
{
uniqueFileIncrement++;
destFile = Path.Combine(Path.GetDirectoryName(sourceFile), value + $" ({uniqueFileIncrement})");
destFile = Path.ChangeExtension(destFile, extension);
if (sourceFile == destFile)
{
// we have gotten back to the name we currently have (don't change it)
break;
}
}
if (sourceFile != destFile)
{
File.Move(sourceFile, destFile);
this.Path = destFile;
this.FilePath = destFile;
ApplicationController.Instance.MainView.Broadcast("ILibraryItem Name Changed", new LibraryItemNameChangedEvent(this.ID));
NameChanged?.Invoke(this, EventArgs.Empty);
}
}
NameChanged?.Invoke(this, EventArgs.Empty);
}
/*
@ -156,6 +170,6 @@ namespace MatterHackers.MatterControl.Library
}
}
public string Path { get; set; }
public string FilePath { get; set; }
}
}