/// <summary> /// http下载文件 /// </summary> /// <param name="url">下载文件地址</param> /// <param name="file">文件存放地址,包含文件名</param> /// <param name="progress">回调进度</param> /// <returns></returns> public static bool Download(string url, string file, ProgressDelegate.ProgressHandler progress = null, object sender = null) { try { string path = Path.GetDirectoryName(file); DirTool.Create(path); //创建文件目录 string tempFile = DirTool.Combine(path, GuidTool.Short() + ".temp"); //临时文件 if (File.Exists(tempFile)) { File.Delete(tempFile); //存在则删除 } FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); // 设置参数 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); //创建本地文件写入流 //Stream stream = new FileStream(tempFile, FileMode.Create); byte[] buffer = new byte[100 * 1024]; int readCount = 0; long filesize = response.ContentLength, current = 0; while ((readCount = responseStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, readCount); current += readCount; if (filesize <= 0 || filesize < current) { if (current > 0) { filesize = current; } else { filesize = 1; } } progress?.Invoke(sender, new ProgressEventArgs(current, filesize)); } //stream.Close(); fs.Close(); responseStream.Close(); File.Delete(file); //删除原始文件 File.Move(tempFile, file); //下载的临时文件重命名 return(true); } catch (Exception ex) { return(false); } }
const string Unknown = "Un";//未知型号相机 /// <summary> /// 创建照片信息模型 /// </summary> /// <param name="file"></param> /// <returns></returns> public static Pictures CreateModel(string file) { try { DateTime createTime = File.GetCreationTime(file); DateTime writeTime = File.GetLastWriteTime(file); FileCodeTool codetool = new FileCodeTool(); string md5 = codetool.GetMD5(file); string sha1 = codetool.GetSHA1(file); Pictures picture = new Pictures() { Id = GuidTool.Short(), ExtName = Path.GetExtension(file).ToUpper(), MD5 = md5, SHA1 = sha1, OrigTime = createTime < writeTime ? createTime : writeTime, }; using (ExifHelper ex = new ExifHelper(file)) { string maker = ex.GetPropertyString((int)ExifTagNames.EquipMake).Trim().Replace(" ", "_"); string model = ex.GetPropertyString((int)ExifTagNames.EquipModel).Trim().Replace(" ", "_"); picture.Model = (Str.Ok(maker) ? maker : Unknown) + "@" + (Str.Ok(model) ? model : Unknown); char GpsLongitudeRef = ex.GetPropertyChar((int)ExifTagNames.GpsLongitudeRef); char GpsLatitudeRef = ex.GetPropertyChar((int)ExifTagNames.GpsLatitudeRef); picture.GpsLongitude = ex.GetPropertyDouble((int)ExifTagNames.GpsLongitude) * (GpsLongitudeRef.Equals('E') ? 1 : -1); picture.GpsLatitude = ex.GetPropertyDouble((int)ExifTagNames.GpsLatitude) * (GpsLatitudeRef.Equals('N') ? 1 : -1); string[] exifDTOrig = ex.GetPropertyString((int)ExifTagNames.ExifDTOrig).Trim().Split(' '); if (exifDTOrig != null && exifDTOrig.Count() == 2) { if (DateTime.TryParse(string.Format("{0} {1}", exifDTOrig[0].Replace(':', '-'), exifDTOrig[1]), out DateTime dt)) { picture.OrigTime = dt; } } picture.Name = string.Format("{0}-{1}{2}", picture.OrigTime.ToString("yyyyMMddhhmmss"), picture.MD5, picture.ExtName); } return(picture); } catch { return(null); } }
/// <summary> /// 更新 /// </summary> /// <param name="info">新版本信息</param> /// <param name="tempPath">文件下载位置</param> /// <param name="dictionary">文件相对位置字典</param> /// <param name="downProgress">下载进度回调</param> /// <param name="downSender">下载进度事件数据</param> /// <param name="releaseProgress">释放进度回调</param> /// <param name="releaseSender">释放进度事件数据</param> /// <returns> /// -10000;//没有新版本 /// -20000;//文件下载失败 /// -30000;//文件释放失败 /// </returns> public int Update(AppUpdateInfo info, string tempPath, Dictionary <string, string> dictionary, ProgressDelegate.ProgressHandler downProgress = null, object downSender = null, ProgressDelegate.ProgressHandler releaseProgress = null, object releaseSender = null) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //请求最新版本信息 if (info != null) { string file = DirTool.Combine(tempPath, GuidTool.Short() + "-" + info.Version); //准备更新(下载) string downfile = Download(file, info, downProgress, downSender); if (!string.IsNullOrWhiteSpace(downfile) && File.Exists(downfile)) { //格式化释放文件目录(相对路径转换为绝对路径) string releasepath = AppDirTool.Get(info.ReleasePath, dictionary); //释放文件,释放完成后删除临时文件 int unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseProgress, releaseSender); File.Delete(file); if (unpackCode > 0) { stopwatch.Stop(); return((int)stopwatch.Elapsed.TotalSeconds); } else { return(-30000 + unpackCode);//文件释放失败 } } else { return(-20000);//文件下载失败 } } else { return(-10000);//没有新版本 } }
/// <summary> /// 初始化Ini配置信息 /// </summary> static void InitIni() { DirTool.Create(R.Paths.Settings); DirTool.Create(R.Paths.Projects); DirTool.Create(R.Paths.DefaultPublishStorage); DirTool.Create(R.Paths.DefaultNewStorage); R.Paths.PublishStorage = IniTool.GetString(R.Files.Settings, "Paths", "PublishStorage", R.Paths.DefaultPublishStorage); if (string.IsNullOrWhiteSpace(R.Paths.PublishStorage)) { R.Paths.PublishStorage = R.Paths.DefaultPublishStorage; } R.Paths.NewStorage = IniTool.GetString(R.Files.Settings, "Paths", "NewStorage", R.Paths.DefaultNewStorage); if (string.IsNullOrWhiteSpace(R.Paths.NewStorage)) { R.Paths.NewStorage = R.Paths.DefaultNewStorage; } R.Tx.IP = IniTool.GetString(R.Files.Settings, "Console", "IP", ""); R.Tx.Port = IniTool.GetInt(R.Files.Settings, "Console", "Port", 0); R.Tx.LocalIP = IniTool.GetString(R.Files.Settings, "Local", "IP", ""); R.Tx.LocalName = IniTool.GetString(R.Files.Settings, "Local", "Name", ""); R.IsAutoDeleteExpiredLog = IniTool.GetBool(R.Files.Settings, "Settings", "AutoDeleteExpiredLog", false); R.AppID = IniTool.GetString(R.Files.Settings, "App", "ID", ""); if (!Str.Ok(R.AppID)) { R.AppID = GuidTool.Short(); IniTool.Set(R.Files.Settings, "App", "ID", R.AppID); } if (!File.Exists(R.Files.NewStorageReadme)) { TxtTool.Create(R.Files.NewStorageReadme, R.NewStorageReadmeTxt); } }