private IDocumentFile AddLocalFile(Uri location, string relativePath = null) { Debug.Assert(location != null); Debug.Assert(location.Scheme == Uri.UriSchemeFile); Debug.Assert(location.IsAbsoluteUri); // check file var sourceFullPath = Path.GetFullPath(location.LocalPath); if (!System.IO.File.Exists(sourceFullPath)) { throw new FileNotFoundException("File not found.", sourceFullPath); } // copy file to working folder and add to list if (string.IsNullOrEmpty(relativePath)) { relativePath = Path.Combine(Guid.NewGuid().ToString(AmlxConstants.GuidFormatFileOrDirectory), Path.GetFileName(sourceFullPath)); } var targetFullPath = ResolveRelativePath(relativePath); System.IO.File.Copy(sourceFullPath, targetFullPath); var file = new IntermediateDocumentFile(_workingDirectory, relativePath); _files.Add(file); RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, file)); return(file); }
internal IDocumentFile AddResourceFile(Package package, string resourceName, string relativePath, DocumentFileType type) { Debug.Assert(resourceName != null); // return if part already exists var uri = new Uri("./" + relativePath, UriKind.Relative); var file = _files.FirstOrDefault(x => x.Location == uri); if (file != null) { return(file); } if (package != null) { uri = PackUriHelper.CreatePartUri(uri); if (package.PartExists(uri)) { var part = package.GetPart(uri); file = new PackageDocumentFile(part) { Type = type }; } } if (file == null) { // copy file to working folder and add to list var targetFullPath = ResolveRelativePath(relativePath); var assembly = typeof(AmlxDocument).Assembly; using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { throw new AmlxException("Could not extract resource file"); } using (var writer = new FileStream(targetFullPath, FileMode.Create, FileAccess.Write)) { stream.CopyTo(writer); } } file = new IntermediateDocumentFile(_workingDirectory, relativePath) { Type = type }; } _files.Add(file); RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, file)); return(file); }