protected string SaveFile(HttpFile file, IRootPathProvider pathProvider) { var uploadDirectory = Path.Combine(pathProvider.GetRootPath(), "Content", "uploads"); if (!Directory.Exists(uploadDirectory)) { Directory.CreateDirectory(uploadDirectory); } var filename = Path.Combine(uploadDirectory, System.Guid.NewGuid().ToString() + file.Name); //HACK creates a unique file name by prepending a Guid using (FileStream fileStream = new FileStream(filename, FileMode.Create)) { file.Value.CopyTo(fileStream); } return filename; }
private int UploadTrackGpx(HttpFile file) { var name = file.Name; using (var reader = XmlReader.Create(file.Value)) { var x = XDocument.Load(reader); XNamespace df = x.Root.Name.Namespace; var xName = x.Element(df.GetName("gpx")).Element(df.GetName("trk")).Element(df.GetName("name")); if (xName != null) name = xName.Value; List<TrackRepository.TrackPoint> points= new List<TrackRepository.TrackPoint>(); var sb = new StringBuilder("LINESTRING("); foreach (var seg in x.Element(df.GetName("gpx")).Element(df.GetName("trk")).Elements(df.GetName("trkseg"))) { foreach (var pt in seg.Elements(df.GetName("trkpt"))) { var p = new TrackRepository.TrackPoint { Lat = float.Parse(pt.Attribute("lat").Value, CultureInfo.InvariantCulture), Lon = float.Parse(pt.Attribute("lon").Value, CultureInfo.InvariantCulture), Elevation = float.Parse(pt.Element(df.GetName("ele")).Value, CultureInfo.InvariantCulture), PointTime = DateTime.Parse(pt.Element(df.GetName("time")).Value, CultureInfo.InvariantCulture), }; points.Add(p); sb.Append(p.Lon.ToString(CultureInfo.InvariantCulture)).Append(" ").Append(p.Lat.ToString(CultureInfo.InvariantCulture)).Append(','); } } //remove last , sb.Remove(sb.Length - 1, 1); sb.Append(")"); ; var trackId = _trackRepository.CreateGpx(Context.CurrentUser().UserId, name, sb.ToString()); _trackRepository.SetPoints(trackId, points.ToArray()); return trackId; } }
private int UploadTrackPlt(HttpFile file) { var sb = new StringBuilder("LINESTRING("); string rawFile = ""; using (StreamReader reader = new StreamReader(file.Value)) { rawFile = reader.ReadToEnd(); file.Value.Position = 0; string line; //skip - first lines for (int i = 0; i < 6; i++) line = reader.ReadLine(); while (!reader.EndOfStream) { line = reader.ReadLine(); var parts = line?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (parts?.Length > 2) sb.Append(parts[1]).Append(" ").Append(parts[0]).Append(','); } //remove last , sb.Remove(sb.Length - 1, 1); sb.Append(")"); } return _trackRepository.CreatePlt(Context.CurrentUser().UserId, file.Name, sb.ToString()); }
private void SaveCMSItem(string key, HttpFile file, DateTime dateToBePublished) { using (var db = DataDocumentStore.DocumentStore.OpenSession()) { var cmsItem = db.Query<CMSItem>().SingleOrDefault(x => x.Key == key); if (cmsItem == null) { cmsItem = new CMSItem { DateCreated = DateTime.Now, Key = key }; } var newVersion = new Version { ContentType = file.ContentType, DateCreated = DateTime.Now, DateToBePublished = dateToBePublished, AttachmentLocation = "content/" + key + "/" + cmsItem.Versions.Count + 1 }; cmsItem.AddVersion(newVersion); db.Store(cmsItem); DataDocumentStore.DocumentStore.DatabaseCommands. PutAttachment(newVersion.AttachmentLocation, null, file.Value, new RavenJObject()); db.SaveChanges(); } }
private Image UpdateImage(IRootPathProvider pathProvider, IRepository repository, Image image, HttpFile newImage) { if (this.HasNewImage(image, newImage)) { // delete the image if (image != null) { image.Delete(); } // add the new image image = new Image() { Filename = newImage.Name }; repository.Add(image); image.SaveImage(newImage.Value); repository.Update(image); } return image; }
private bool HasNewImage(Image image, HttpFile newImage) { return ((newImage != null) && (newImage.ContentType.StartsWith("image") && ((image == null) || (image.Filename != newImage.Name)))); }
private bool UploadFile(HttpFile httpFile, TextWriter responseWriter) { var unpackPath = UnpackDestination; if (!Directory.Exists(unpackPath)) Directory.CreateDirectory(unpackPath); var packageName = Path.GetFileNameWithoutExtension(httpFile.Name); if (packageName == null) { responseWriter.WriteLine("Cannot get filename without extension from uploaded file: " + httpFile.Name); return false; } var logFile = Path.Combine(unpackPath, packageName + ".log"); var unpackDestination = Path.Combine(unpackPath, packageName); using (var fileStream = File.OpenWrite(logFile)) using (var fileWriter = new StreamWriter(fileStream)) using (var logWriter = new TextWriterProxy(responseWriter, fileWriter)) { try { DeleteOldArtifacts(logWriter); var command = new DeployZipFileCommand(logWriter, unpackDestination); command.Execute(httpFile.Value); return true; } catch (Exception error) { logWriter.WriteLine(error); return false; } } }