private void ReNameFile(FileInfo srcFileInfo, bool moveFile = true) { string file = srcFileInfo.FullName; ExifToolWrapper exif = new ExifToolWrapper(); exif.Run(file); string cameraModel = string.Empty; string cameraDateTime = string.Empty; ExifTagItem model = exif.Find("Camera Model Name"); if (string.IsNullOrEmpty(model.value)) { model = exif.Find("Model"); } cameraModel = model.value; ExifTagItem creationDate = exif.Find("Date/Time Original"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Creation Date"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Create Date"); if (string.IsNullOrEmpty(creationDate.value)) { creationDate = exif.Find("Date Created"); } } } cameraDateTime = creationDate.value; if (string.IsNullOrEmpty(cameraModel)) { cameraModel = "未知"; } if (string.IsNullOrEmpty(cameraDateTime)) { return; } if (cameraDateTime.Contains("+08:00")) { cameraDateTime = cameraDateTime.Replace("+08:00", ""); } //文件名形式 照相机型号\年\月\日\年月日_时分秒_00 IFormatProvider ifp = new CultureInfo("zh-CN", true); DateTime dateTime = DateTime.ParseExact(cameraDateTime, "yyyy:MM:dd HH:mm:ss", ifp); string fileName = $"{cameraModel}\\{dateTime.Year}\\{dateTime.Month.ToString().PadLeft(2, '0')}\\{dateTime.Day.ToString().PadLeft(2, '0')}\\{dateTime.Year}{dateTime.Month.ToString().PadLeft(2, '0')}{dateTime.Day.ToString().PadLeft(2, '0')}_{dateTime.Hour.ToString().PadLeft(2, '0')}{dateTime.Minute.ToString().PadLeft(2, '0')}{dateTime.Second.ToString().PadLeft(2, '0')}_00"; string dstFile = Path.Combine(dstText.Text, fileName + srcFileInfo.Extension); FileInfo dstFileInfo = new FileInfo(dstFile); if (!dstFileInfo.Directory.Exists) { dstFileInfo.Directory.Create(); } if (!dstFileInfo.Exists) { if (moveFile) { File.Move(srcFileInfo.FullName, dstFileInfo.FullName); } else { File.Copy(srcFileInfo.FullName, dstFileInfo.FullName); } } else { //比较md5 string srcMd5 = md5File(srcFileInfo.FullName); string dstMd5 = md5File(dstFileInfo.FullName); if (srcMd5 == dstMd5) { if (moveFile) { srcFileInfo.Delete(); } return; } //文件添加后缀 int num = 0; while (true) { num++; dstFile = Path.Combine(dstText.Text, fileName + num.ToString().PadLeft(2, '0') + srcFileInfo.Extension); dstFileInfo = new FileInfo(dstFile); if (!dstFileInfo.Exists) { if (moveFile) { File.Move(srcFileInfo.FullName, dstFileInfo.FullName); } else { File.Copy(srcFileInfo.FullName, dstFileInfo.FullName); } return; } else { dstMd5 = md5File(dstFileInfo.FullName); if (srcMd5 == dstMd5) { if (moveFile) { srcFileInfo.Delete(); } return; } } } } }
private async Task <IActionResult> AnalyzeFile(IFormFile ffile, String filePath, String thmFilePath, PhotoViewModelEx updrst, String usrName) { Boolean bThumbnailCreated = false; using (var fileStream = new FileStream(filePath, FileMode.Create)) { await ffile.CopyToAsync(fileStream); try { ExifToolWrapper wrap = new ExifToolWrapper(); wrap.Run(filePath); foreach (var item in wrap) { #if DEBUG System.Diagnostics.Debug.WriteLine("{0}, {1}, {2}", item.group, item.name, item.value); #endif if (item.group == "EXIF" || item.group == "Composite" || item.group == "XMP") { updrst.ExifTags.Add(item); } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif _logger.LogError(exp.Message); } try { using (MagickImage image = new MagickImage(filePath)) { updrst.Width = image.Width; updrst.Height = image.Height; // Retrieve the exif information ExifProfile profile = image.GetExifProfile(); if (profile != null) { using (MagickImage thumbnail = profile.CreateThumbnail()) { // Check if exif profile contains thumbnail and save it if (thumbnail != null) { thumbnail.Write(thmFilePath); updrst.ThumbWidth = thumbnail.Width; updrst.ThumbHeight = thumbnail.Height; bThumbnailCreated = true; } } } if (!bThumbnailCreated) { MagickGeometry size = new MagickGeometry(256, 256); // This will resize the image to a fixed size without maintaining the aspect ratio. // Normally an image will be resized to fit inside the specified size. size.IgnoreAspectRatio = false; image.Resize(size); updrst.ThumbWidth = image.Width; updrst.ThumbHeight = image.Height; // Save the result image.Write(thmFilePath); } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif _logger.LogError(exp.Message); updrst.success = false; updrst.error = exp.Message; } } updrst.OrgFileName = ffile.FileName; updrst.UploadedTime = DateTime.Now; updrst.IsOrgThumbnail = bThumbnailCreated; updrst.UploadedBy = usrName; return(Json(true)); }