diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Archiver/AssetsArchiver.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Archiver/AssetsArchiver.cs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/World/Archiver/AssetsArchiver.cs b/OpenSim/Region/CoreModules/World/Archiver/AssetsArchiver.cs new file mode 100644 index 0000000..76d27ce --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Archiver/AssetsArchiver.cs | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System.Collections.Generic; | ||
29 | using System.IO; | ||
30 | using System.Reflection; | ||
31 | using System.Xml; | ||
32 | using OpenMetaverse; | ||
33 | using log4net; | ||
34 | using OpenSim.Framework; | ||
35 | |||
36 | namespace OpenSim.Region.CoreModules.World.Archiver | ||
37 | { | ||
38 | /// <summary> | ||
39 | /// Archives assets | ||
40 | /// </summary> | ||
41 | public class AssetsArchiver | ||
42 | { | ||
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | /// <summary> | ||
46 | /// Archive assets | ||
47 | /// </summary> | ||
48 | protected IDictionary<UUID, AssetBase> m_assets; | ||
49 | |||
50 | public AssetsArchiver(IDictionary<UUID, AssetBase> assets) | ||
51 | { | ||
52 | m_assets = assets; | ||
53 | } | ||
54 | |||
55 | /// <summary> | ||
56 | /// Archive the assets given to this archiver to the given archive. | ||
57 | /// </summary> | ||
58 | /// <param name="archive"></param> | ||
59 | public void Archive(TarArchiveWriter archive) | ||
60 | { | ||
61 | //WriteMetadata(archive); | ||
62 | WriteData(archive); | ||
63 | } | ||
64 | |||
65 | /// <summary> | ||
66 | /// Write an assets metadata file to the given archive | ||
67 | /// </summary> | ||
68 | /// <param name="archive"></param> | ||
69 | protected void WriteMetadata(TarArchiveWriter archive) | ||
70 | { | ||
71 | StringWriter sw = new StringWriter(); | ||
72 | XmlTextWriter xtw = new XmlTextWriter(sw); | ||
73 | |||
74 | xtw.Formatting = Formatting.Indented; | ||
75 | xtw.WriteStartDocument(); | ||
76 | |||
77 | xtw.WriteStartElement("assets"); | ||
78 | |||
79 | foreach (UUID uuid in m_assets.Keys) | ||
80 | { | ||
81 | AssetBase asset = m_assets[uuid]; | ||
82 | |||
83 | if (asset != null) | ||
84 | { | ||
85 | xtw.WriteStartElement("asset"); | ||
86 | |||
87 | string extension = string.Empty; | ||
88 | |||
89 | if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Metadata.Type)) | ||
90 | { | ||
91 | extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Metadata.Type]; | ||
92 | } | ||
93 | |||
94 | xtw.WriteElementString("filename", uuid.ToString() + extension); | ||
95 | |||
96 | xtw.WriteElementString("name", asset.Metadata.Name); | ||
97 | xtw.WriteElementString("description", asset.Metadata.Description); | ||
98 | xtw.WriteElementString("asset-type", asset.Metadata.Type.ToString()); | ||
99 | |||
100 | xtw.WriteEndElement(); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | xtw.WriteEndElement(); | ||
105 | |||
106 | xtw.WriteEndDocument(); | ||
107 | |||
108 | archive.AddFile("assets.xml", sw.ToString()); | ||
109 | } | ||
110 | |||
111 | /// <summary> | ||
112 | /// Write asset data files to the given archive | ||
113 | /// </summary> | ||
114 | /// <param name="archive"></param> | ||
115 | protected void WriteData(TarArchiveWriter archive) | ||
116 | { | ||
117 | // It appears that gtar, at least, doesn't need the intermediate directory entries in the tar | ||
118 | //archive.AddDir("assets"); | ||
119 | |||
120 | foreach (UUID uuid in m_assets.Keys) | ||
121 | { | ||
122 | AssetBase asset = m_assets[uuid]; | ||
123 | |||
124 | string extension = string.Empty; | ||
125 | |||
126 | if (ArchiveConstants.ASSET_TYPE_TO_EXTENSION.ContainsKey(asset.Metadata.Type)) | ||
127 | { | ||
128 | extension = ArchiveConstants.ASSET_TYPE_TO_EXTENSION[asset.Metadata.Type]; | ||
129 | } | ||
130 | else | ||
131 | { | ||
132 | m_log.ErrorFormat( | ||
133 | "[ARCHIVER]: Unrecognized asset type {0} with uuid {1}. This asset will be saved but not reloaded", | ||
134 | asset.Metadata.Type, asset.Metadata.ID); | ||
135 | } | ||
136 | |||
137 | archive.AddFile( | ||
138 | ArchiveConstants.ASSETS_PATH + uuid.ToString() + extension, | ||
139 | asset.Data); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | } | ||