Skip to content

Commit

Permalink
Merge branch 'wip-lnk'
Browse files Browse the repository at this point in the history
  • Loading branch information
yfdyh000 committed Apr 9, 2022
2 parents f717da3 + 563fe40 commit 9afa274
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
5 changes: 4 additions & 1 deletion PhotoSift/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ public Color CustomMenuColorHightlight
public FeatureSwitch FileMIMEChecker { get; set; }
[Category("File Type"), DisplayName("Allowed MIME"), DescriptionAttribute("File MIME types allowed to be added to the pool. Work only while the Check MIME option be turn on. Semicolon separated. Spaces on edge are ignored. Default: 'image/;video/;audio/'.")]
public string allowsMIME { get; set; }
[Category("File Type"), DisplayName("Expand folder shortcuts"), DescriptionAttribute("When adding files, the program will only parse one folder shortcut (if it is the first) to avoid flooding. If enabled, it attempts to recursively resolve all folder shortcuts.")]
public bool expandFolderLnks { get; set; }

// Misc settings
[Category("Misc"), DisplayName("Copy action"), DescriptionAttribute("Sets the action type when you press Ctrl+C or click the \"Copy to clipboard\" menu in this software.")]
Expand Down Expand Up @@ -518,7 +520,8 @@ public AppSettings()
defaultSettings.Add("allowsVidExts", Util.Def_allowsVideoExts);
defaultSettings.Add("FileMIMEChecker", FeatureSwitch.Disabled);
defaultSettings.Add("allowsMIME", "image/);video/);audio/");

defaultSettings.Add("expandFolderLnks", false);

// Misc
defaultSettings.Add("SaveRelativePaths", true);
defaultSettings.Add("CopyActionType", CopytoClipboardOptions.Bitmap);
Expand Down
9 changes: 9 additions & 0 deletions PhotoSift/PhotoSift.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@
<WrapperTool>aximp</WrapperTool>
<Isolated>False</Isolated>
</COMReference>
<COMReference Include="IWshRuntimeLibrary">
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>
<COMReference Include="Shell32">
<Guid>{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}</Guid>
<VersionMajor>1</VersionMajor>
Expand Down
96 changes: 70 additions & 26 deletions PhotoSift/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,37 +294,57 @@ private void frmMain_DragDrop( object sender, DragEventArgs e )
}

// Add file to the image pool
private void _addFile(string file, ref List<string> newPics, List<string> allowsExts)
private void _addFile(string filePath, ref List<string> newPics, List<string> allowsExts)
{
if (pics.Exists(i => i == file)) return;
if (pics.Exists(i => i == filePath)) return;
string ext = Path.GetExtension(filePath);

if (ext.ToLower() == ".lnk")
{
string targetPath = GetLnkFileTarget(filePath);
if (System.IO.Directory.Exists(targetPath))
{
// refuse to expand multiple lnks at once
if (settings.expandFolderLnks || newPics.Count < 1)
{
var files = System.IO.Directory.EnumerateFiles(targetPath, "*", System.IO.SearchOption.AllDirectories).ToArray();
foreach (var file in files)
{
_addFiles_AddingByMIME(file, ref newPics, allowsExts);
}
}
}
else
{
filePath = targetPath;
if (pics.Exists(i => i == filePath)) return;
_addFiles_AddingByMIME(filePath, ref newPics, allowsExts);
}
return;
}
_addFiles_AddingByMIME(filePath, ref newPics, allowsExts);
}
private void _addFiles_AddingByMIME(string filePath, ref List<string> newPics, List<string> allowsExts) {
string ext = Path.GetExtension(filePath);
if (settings.FileMIMEChecker == FeatureSwitch.Enabled && allowsMIME.Length > 0)
{
string mime = GetFileMIME(file);
string mime = MimeTypes.MimeTypeMap.GetMimeType(ext);

int match = allowsMIME.Where(rule => mime.Contains(rule)).ToArray().Length;
if (match > 0)
newPics.Add(file);
newPics.Add(filePath);
}
else if (allowsExts.Contains(Path.GetExtension(file), StringComparer.OrdinalIgnoreCase))
newPics.Add(file);
else if (allowsExts.Contains(ext, StringComparer.OrdinalIgnoreCase))
newPics.Add(filePath);
}
private int AddFiles( string[] items )
private List<string> _addFiles_validate(string[] items)
{
if( items.Length == 0 ) return 0;

HaltAutoAdvance();
lblHeader.Text = "Loading...";
updateTitleStr("Loading...");
Util.CenterControl( lblHeader, picLogo.Image.Height / 2 + 20 );
this.Refresh();

// validate files to add
List<string> newPics = new List<string>();
List<string> allowsExts = settings.allowsPicExts.Union(settings.allowsVidExts).ToList();
foreach ( string item in items )
foreach (string item in items)
{
if( System.IO.Directory.Exists( item ) ) // is Directory
if (System.IO.Directory.Exists(item)) // is Directory
{
try
{
Expand All @@ -336,16 +356,33 @@ private int AddFiles( string[] items )
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // lising dir error, e.g. "C:\".
return -1;
return null;
}
}
else if( System.IO.File.Exists( item ) ) // is File
else if (System.IO.File.Exists(item)) // is File
{
_addFile(item, ref newPics, allowsExts);
}

}
if( newPics.Count == 0 )
return newPics;
}
private int AddFiles( string[] items )
{
if( items.Length == 0 ) return 0;

HaltAutoAdvance();
lblHeader.Text = "Loading...";
updateTitleStr("Loading...");
Util.CenterControl( lblHeader, picLogo.Image.Height / 2 + 20 );
this.Refresh();

var newPics = _addFiles_validate(items);

if (newPics == null)
{
return 0;
}
else if ( newPics.Count == 0 )
{
ShowPicByOffset( 0 );
return 0;
Expand Down Expand Up @@ -1598,11 +1635,18 @@ private void timerMetaInfoUpdate_Tick(object sender, EventArgs e)
updateInfoLabel(str);
}

public static string GetFileMIME(string filePath)
{
var ext = Path.GetExtension(filePath);
return MimeTypes.MimeTypeMap.GetMimeType(ext);
public static string GetLnkFileTarget(string filePath)
{
try
{
IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut link = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filePath);
return link.TargetPath;
}
catch (Exception)
{
return "";
}
}
}

}

0 comments on commit 9afa274

Please sign in to comment.