-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
148 lines (130 loc) · 5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ExportArenaFiles
{
class Program
{
static IDbConnection Connection;
static void Main( string[] args )
{
string serverName = "localhost";
string databaseName = "ArenaDB";
string exportFolder = ".\\";
string connectionString;
IDbCommand command;
IDataReader reader;
if ( args.Length > 0 )
{
exportFolder = args[0];
if ( args.Length > 1 )
{
serverName = args[1];
if ( args.Length > 2 )
{
databaseName = args[2];
}
}
}
connectionString = string.Format( "Data Source={0}; Initial Catalog={1}; Integrated Security=SSPI; MultipleActiveResultSets=True;", serverName, databaseName );
using ( Connection = new SqlConnection( connectionString ) )
{
Connection.Open();
using ( command = Connection.CreateCommand() )
{
command.CommandText = "SELECT folder_id,title FROM file_folder WHERE parent_folder_id = -1";
using ( reader = command.ExecuteReader() )
{
while ( reader.Read() )
{
ExportFolder( ( int ) reader[0], WebUtility.HtmlDecode( ( string ) reader[1] ), exportFolder );
}
}
}
}
Console.WriteLine( "Finished" );
Console.ReadKey();
}
static void ExportFolder( int folderId, string folderName, string exportFolder )
{
IDbCommand command;
IDataReader reader;
string targetPath;
//
// Create the folder.
//
targetPath = Path.Combine( exportFolder, SafeFileName( folderName ) );
if ( !Directory.Exists( targetPath ) )
{
Directory.CreateDirectory( targetPath );
}
//
// Process sub-folders.
//
using ( command = Connection.CreateCommand() )
{
command.CommandText = "SELECT folder_id,title FROM file_folder WHERE parent_folder_id = @folderId";
command.Parameters.Add( new SqlParameter( "@folderId", folderId ) );
using ( reader = command.ExecuteReader() )
{
while ( reader.Read() )
{
ExportFolder( ( int ) reader[0], WebUtility.HtmlDecode( ( string ) reader[1] ), targetPath );
}
}
}
//
// Process files.
//
using ( command = Connection.CreateCommand() )
{
command.CommandText = "SELECT blob_id FROM file_folder_document WHERE document_folder_id = @folderId";
command.Parameters.Add( new SqlParameter( "@folderId", folderId ) );
using ( reader = command.ExecuteReader() )
{
while ( reader.Read() )
{
ExportFile( ( int ) reader[0], targetPath );
}
}
}
}
static void ExportFile( int blobId, string exportFolder )
{
IDbCommand command;
IDataReader reader;
//
// Process blob.
//
using ( command = Connection.CreateCommand() )
{
command.CommandText = "SELECT original_file_name,blob FROM util_blob WHERE blob_id = @blobId";
command.Parameters.Add( new SqlParameter( "@blobId", blobId ) );
using ( reader = command.ExecuteReader() )
{
if ( reader.Read() )
{
string filename = Path.Combine( exportFolder, SafeFileName( ( string ) reader[0] ) );
File.WriteAllBytes( filename, ( byte[] ) reader[1] );
Console.WriteLine( string.Format( "Processed: {0}", filename ) );
}
}
}
}
static string SafeFileName( string filename )
{
string safeFilename = filename.Replace( '/', '_' ).Replace( '\\', '_' ).Replace( '?', '_' ).Replace( '*', '_' ).Replace( ':', '_' ).Replace( '|', '_' ).Replace( '"', '_' ).Replace( '<', '_' ).Replace( '>', '_' );
while ( safeFilename.EndsWith( "." ) || safeFilename.EndsWith( " " ) )
{
safeFilename = safeFilename.Substring( 0, safeFilename.Length - 1 );
}
return safeFilename;
}
}
}