private static void CopyOneFile(Uri destination, IPublishFile item) { var destFile = CommonUtils.GetAbsoluteFilePath(destination.LocalPath, item.DestinationFile); Debug.WriteLine("CopyingOneFile: " + destFile); string destDir = Path.GetDirectoryName(destFile); if (!Directory.Exists(destDir)) { // don't create a file share (\\fob\oar) if (!Path.IsPathRooted(destDir) || Path.GetPathRoot(destDir) != destDir) { Directory.CreateDirectory(destDir); Debug.WriteLine("Created dir: " + destDir); } } File.Copy(item.SourceFile, destFile, true); Debug.WriteLine("Copied file: " + destFile); // Attempt to remove read-only attribute from the destination file. try { var attr = File.GetAttributes(destFile); if (attr.HasFlag(FileAttributes.ReadOnly)) { File.SetAttributes(destFile, attr & ~FileAttributes.ReadOnly); Debug.WriteLine("Removed read-only attribute."); } } catch (IOException) { } catch (UnauthorizedAccessException) { } }
private static void CopyOneFile(Uri destination, IPublishFile item) { var destFile = item.DestinationFile; // get the destination file URI, the root path, and the destination directory string newLoc = "ftp://"; if (!String.IsNullOrEmpty(destination.UserInfo)) { newLoc += destination.UserInfo + "@"; } newLoc += destination.Host; string rootPath = newLoc + "/"; newLoc += destination.AbsolutePath + "/" + destFile.Replace('\\', '/') + destination.Query; string destinationDir = Path.GetDirectoryName(Path.Combine(destination.AbsolutePath.Replace('/', '\\'), destFile)); EnsureDirectoryExists(rootPath, destinationDir); // upload the file FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(newLoc)); request.Method = WebRequestMethods.Ftp.UploadFile; byte[] buffer = new byte[1024]; FileStream stream = new FileStream(item.SourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); int bytesRead; request.ContentLength = stream.Length; var reqStream = request.GetRequestStream(); while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { reqStream.Write(buffer, 0, bytesRead); } reqStream.Close(); var response = (FtpWebResponse)request.GetResponse(); if (response.StatusCode != FtpStatusCode.ClosingData) { throw new IOException(Strings.FtpPublisherUploadFileException.FormatUI(response.StatusDescription)); } }
private static void CopyOneFile(Uri destination, IPublishFile item, ref ImpersonationHelper impersonate, NetworkCredential creds) { var destFile = Path.Combine(destination.LocalPath, item.DestinationFile); string destDir = Path.GetDirectoryName(destFile); if (!Directory.Exists(destDir)) { // don't create a file share (\\foo\bar) if (!Path.IsPathRooted(destDir) || Path.GetPathRoot(destDir) != destDir) { Directory.CreateDirectory(destDir); } } if (impersonate != null) { // we need to unimpersonate to read the file, then re-impersonate when we're done. using (FileStream destStream = new FileStream(destFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { impersonate.UndoImpersonate(); impersonate = null; try { using (FileStream file = new FileStream(item.SourceFile, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0) { destStream.Write(buffer, 0, bytesRead); } } } finally { impersonate = new ImpersonationHelper(creds); } } } else { File.Copy(item.SourceFile, destFile, true); } }
private static void CopyOneFile(Uri destination, IPublishFile item) { var destFile = item.DestinationFile; // get the destination file URI, the root path, and the destination directory string newLoc = "ftp://"; if (!String.IsNullOrEmpty(destination.UserInfo)) { newLoc += destination.UserInfo + "@"; } newLoc += destination.Host; string rootPath = newLoc + "/"; newLoc += destination.AbsolutePath + "/" + destFile.Replace('\\', '/') + destination.Query; string destinationDir = Path.GetDirectoryName(Path.Combine(destination.AbsolutePath.Replace('/', '\\'), destFile)); EnsureDirectoryExists(rootPath, destinationDir); // upload the file FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(newLoc)); request.Method = WebRequestMethods.Ftp.UploadFile; byte[] buffer = new byte[1024]; FileStream stream = new FileStream(item.SourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); int bytesRead; request.ContentLength = stream.Length; var reqStream = request.GetRequestStream(); while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { reqStream.Write(buffer, 0, bytesRead); } reqStream.Close(); var response = (FtpWebResponse)request.GetResponse(); if (response.StatusCode != FtpStatusCode.ClosingData) { throw new IOException(String.Format("Failed to upload file: {0}", response.StatusDescription)); } }
public PublishProjectOptions(IPublishFile[] additionalFiles = null, string destinationUrl = null) { _additionalFiles = additionalFiles ?? Default._additionalFiles; _destination = destinationUrl; }
private static void CopyOneFile(Uri destination, IPublishFile item) { var destFile = CommonUtils.GetAbsoluteFilePath(destination.LocalPath, item.DestinationFile); Debug.WriteLine("CopyingOneFile: " + destFile); string destDir = Path.GetDirectoryName(destFile); if (!Directory.Exists(destDir)) { // don't create a file share (\\foo\bar) if (!Path.IsPathRooted(destDir) || Path.GetPathRoot(destDir) != destDir) { Directory.CreateDirectory(destDir); Debug.WriteLine("Created dir: " + destDir); } } File.Copy(item.SourceFile, destFile, true); Debug.WriteLine("Copied file: " + destFile); }