public void SaveFile(SaveFileParameter param) { Guid checksum; string path; if (!TryGetPath(param, out checksum, out path)) { return; } File.WriteAllBytes(path, param.FileData); var repository = new InfrastructureRepository(); repository.SaveFile(checksum.ToString(), param.FileName, path); }
private static bool TryGetPath(SaveFileParameter param, out Guid checksum, out string path) { checksum = CryptoManaged.MD5Hash(new MemoryStream(param.FileData)); path = string.Format(@"{0}{1}\{2}{3}", InfrastructureRepository.RootPath, param.AppID.ToString("N"), checksum, Path.GetExtension(param.FileName)); var file = new FileInfo(path); FileStream x = null; try { return(!(file.Exists && CryptoManaged.MD5Hash(x = file.OpenRead()) == checksum)); } finally { if (x != null) { x.Close(); } } }