public static bool TryCreate(BackendCompiler backend, File file, out PackageFile packageFile) { packageFile = null; string path; if (backend.FileManager.TryResolvePath(file.Source, out path)) { string mimeType = "application/x-msdownload"; // TODO: pick appropriate MIME type. string partUri = PackageFile.ConvertToPartUri(backend, file.Path); packageFile = new PackageFile() { File = file, MimeType = mimeType, PartUri = partUri, SourcePath = path }; } else { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.FileNotFound(file.Source), file)); } return(packageFile != null); }
/// <summary> /// Copy or move a single transfer. /// </summary> /// <param name="backend">Compiler backend that generated the transfers.</param> /// <param name="transfer">File transfers to execute.</param> public static void ExecuteTransfer(BackendCompiler backend, FileTransfer transfer) { if (transfer.Redundant) { // TODO: log that we tried to transfer a redundant file? return; } bool retry = false; do { try { if (transfer.Move) { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.MoveFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Move(transfer.Source, transfer.Destination); } else { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.CopyFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Copy(transfer.Source, transfer.Destination, true); } retry = false; } catch (FileNotFoundException e) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.FileNotFound(transfer.Source), transfer.LineNumber), e); } catch (DirectoryNotFoundException dnfe) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), dnfe); } string directory = Path.GetDirectoryName(transfer.Destination); Directory.CreateDirectory(directory); retry = true; } catch (UnauthorizedAccessException uae) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), uae); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } catch (IOException e) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), e); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } } while (retry); }