The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. namespace Teknik.Migrations
  2. {
  3. using Areas.Paste;
  4. using Areas.Upload;
  5. using Helpers;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.Entity;
  9. using System.Data.Entity.Migrations;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using Teknik.Configuration;
  14. internal sealed class Configuration : DbMigrationsConfiguration<Models.TeknikEntities>
  15. {
  16. public Configuration()
  17. {
  18. AutomaticMigrationsEnabled = true;
  19. AutomaticMigrationDataLossAllowed = true;
  20. }
  21. protected override void Seed(Models.TeknikEntities context)
  22. {
  23. // Pre-populate with the default stuff
  24. // Create system blog
  25. /*
  26. Config config = Config.Load();
  27. Areas.Profile.Models.User systemUser = new Areas.Profile.Models.User();
  28. systemUser.Username = Constants.SERVERUSER;
  29. systemUser.JoinDate = DateTime.Now;
  30. systemUser.LastSeen = DateTime.Now;
  31. systemUser.UserSettings = new Areas.Profile.Models.UserSettings();
  32. systemUser.BlogSettings = new Areas.Profile.Models.BlogSettings();
  33. systemUser.UploadSettings = new Areas.Profile.Models.UploadSettings();
  34. context.Users.AddOrUpdate(systemUser);
  35. context.SaveChanges();
  36. Areas.Blog.Models.Blog systemBlog = new Areas.Blog.Models.Blog();
  37. systemBlog.UserId = systemUser.UserId;
  38. systemBlog.BlogId = config.BlogConfig.ServerBlogId;
  39. context.Blogs.AddOrUpdate(systemBlog);
  40. context.SaveChanges();
  41. // Create roles and groups
  42. Areas.Profile.Models.Role adminRole = new Areas.Profile.Models.Role();
  43. adminRole.Name = "Admin";
  44. adminRole.Description = "Allows complete access to user specific actions";
  45. context.Roles.AddOrUpdate(adminRole);
  46. Areas.Profile.Models.Role podcastRole = new Areas.Profile.Models.Role();
  47. podcastRole.Name = "Podcast";
  48. podcastRole.Description = "Allows create/edit/delete access to podcasts";
  49. context.Roles.AddOrUpdate(podcastRole);
  50. Areas.Profile.Models.Group adminGroup = new Areas.Profile.Models.Group();
  51. adminGroup.Name = "Administrators";
  52. adminGroup.Description = "System Administrators with full access";
  53. adminGroup.Roles = new List<Areas.Profile.Models.Role>();
  54. adminGroup.Roles.Add(adminRole);
  55. adminGroup.Roles.Add(podcastRole);
  56. context.Groups.AddOrUpdate(adminGroup);
  57. Areas.Profile.Models.Group podcastGroup = new Areas.Profile.Models.Group();
  58. podcastGroup.Name = "Podcast";
  59. podcastGroup.Description = "Podcast team members";
  60. podcastGroup.Roles = new List<Areas.Profile.Models.Role>();
  61. podcastGroup.Roles.Add(podcastRole);
  62. context.Groups.AddOrUpdate(podcastGroup);
  63. Areas.Profile.Models.Group memberGroup = new Areas.Profile.Models.Group();
  64. memberGroup.Name = "Member";
  65. memberGroup.Description = "The default member group with basic permissions";
  66. context.Groups.AddOrUpdate(memberGroup);
  67. context.SaveChanges();
  68. if (config.DatabaseConfig.Migrate && !config.DevEnvironment)
  69. {
  70. // Convert legacy MySQL DB to new MS SQL DB
  71. MysqlDatabase db = new MysqlDatabase(config.DatabaseConfig);
  72. db.MysqlErrorEvent += Db_MysqlErrorEvent;
  73. config.DatabaseConfig.Migrate = false;
  74. Config.Save(config);
  75. // Transfer transactions
  76. var transRet = db.Query("SELECT * FROM `transactions`");
  77. foreach (var tran in transRet)
  78. {
  79. switch (tran["trans_type"].ToString())
  80. {
  81. case "One-Time":
  82. Areas.Transparency.Models.OneTime tr = new Areas.Transparency.Models.OneTime();
  83. tr.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  84. tr.Amount = Double.Parse(tran["amount"].ToString());
  85. tr.Currency = tran["currency"].ToString();
  86. tr.Recipient = tran["recipient"].ToString();
  87. tr.Reason = tran["reason"].ToString();
  88. context.Transactions.AddOrUpdate(tr);
  89. break;
  90. case "Bill":
  91. Areas.Transparency.Models.Bill bill = new Areas.Transparency.Models.Bill();
  92. bill.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  93. bill.Amount = Double.Parse(tran["amount"].ToString());
  94. bill.Currency = tran["currency"].ToString();
  95. bill.Recipient = tran["recipient"].ToString();
  96. bill.Reason = tran["reason"].ToString();
  97. context.Transactions.AddOrUpdate(bill);
  98. break;
  99. case "Donation":
  100. Areas.Transparency.Models.Donation don = new Areas.Transparency.Models.Donation();
  101. don.DateSent = DateTime.Parse(tran["date_posted"].ToString());
  102. don.Amount = Double.Parse(tran["amount"].ToString());
  103. don.Currency = tran["currency"].ToString();
  104. don.Sender = tran["sender"].ToString();
  105. don.Reason = tran["reason"].ToString();
  106. context.Transactions.AddOrUpdate(don);
  107. break;
  108. }
  109. }
  110. context.SaveChanges();
  111. // Transfer Users and Blogs/Posts
  112. Dictionary<int, int> userMapping = new Dictionary<int, int>();
  113. Dictionary<int, int> postMapping = new Dictionary<int, int>();
  114. var userRet = db.Query("SELECT * FROM `users`");
  115. foreach (var user in userRet)
  116. {
  117. // Create User
  118. Areas.Profile.Models.User newUser = new Areas.Profile.Models.User();
  119. newUser.UserSettings = new Areas.Profile.Models.UserSettings();
  120. newUser.BlogSettings = new Areas.Profile.Models.BlogSettings();
  121. newUser.UploadSettings = new Areas.Profile.Models.UploadSettings();
  122. newUser.TransferAccount = true;
  123. newUser.Username = user["username"].ToString();
  124. newUser.HashedPassword = user["password"].ToString();
  125. newUser.JoinDate = DateTime.Parse(user["join_date"].ToString());
  126. newUser.LastSeen = DateTime.Parse(user["join_date"].ToString());
  127. newUser.UserSettings.About = user["about"].ToString();
  128. newUser.UserSettings.Website = user["website"].ToString();
  129. newUser.UserSettings.Quote = user["quote"].ToString();
  130. newUser.BlogSettings.Title = user["blog_title"].ToString();
  131. newUser.BlogSettings.Description = user["blog_desc"].ToString();
  132. if (user["site_admin"].ToString() == "1")
  133. {
  134. newUser.Groups.Add(adminGroup);
  135. }
  136. context.Users.AddOrUpdate(newUser);
  137. context.SaveChanges();
  138. string oldUsername = user["username"].ToString();
  139. Areas.Profile.Models.User newUser = context.Users.Where(u => u.Username == oldUsername).FirstOrDefault();
  140. if (newUser != null)
  141. {
  142. int oldUserId = Int32.Parse(user["id"].ToString());
  143. int userId = newUser.UserId;
  144. userMapping.Add(oldUserId, userId);
  145. }
  146. // Create Blog for user
  147. Areas.Blog.Models.Blog newBlog = new Areas.Blog.Models.Blog();
  148. newBlog.UserId = userId;
  149. context.Blogs.AddOrUpdate(newBlog);
  150. context.SaveChanges();
  151. int blogId = newBlog.BlogId;
  152. // Transfer Blog Posts
  153. var postRet = db.Query("SELECT * FROM `blog` WHERE `author_id` = {0}", new object[] { oldUserId });
  154. if (postRet != null)
  155. {
  156. foreach (var post in postRet)
  157. {
  158. // Create new Blog Post
  159. Areas.Blog.Models.BlogPost newPost = new Areas.Blog.Models.BlogPost();
  160. if (post["user_id"].ToString() == "0")
  161. {
  162. newPost.BlogId = config.BlogConfig.ServerBlogId;
  163. newPost.System = true;
  164. }
  165. else
  166. {
  167. newPost.BlogId = blogId;
  168. }
  169. newPost.DatePosted = DateTime.Parse(post["date_posted"].ToString());
  170. DateTime publishDate = DateTime.Now;
  171. DateTime.TryParse(post["date_published"].ToString(), out publishDate);
  172. if (publishDate < newPost.DatePosted)
  173. {
  174. publishDate = newPost.DatePosted;
  175. }
  176. newPost.DatePublished = publishDate;
  177. newPost.DateEdited = publishDate;
  178. newPost.Published = (post["published"].ToString() == "True");
  179. newPost.Title = post["title"].ToString();
  180. newPost.Article = post["post"].ToString();
  181. context.BlogPosts.AddOrUpdate(newPost);
  182. context.SaveChanges();
  183. postMapping.Add(Int32.Parse(post["id"].ToString()), newPost.BlogPostId);
  184. }
  185. }
  186. }
  187. // Transfer Blog Comments
  188. var commentRet = db.Query("SELECT * FROM `comments` WHERE `service` = {0}", new object[] { "blog" });
  189. foreach (var comment in commentRet)
  190. {
  191. int postId = Int32.Parse(comment["reply_id"].ToString());
  192. int userId = Int32.Parse(comment["user_id"].ToString());
  193. if (postMapping.ContainsKey(postId) && userMapping.ContainsKey(userId))
  194. {
  195. Areas.Blog.Models.BlogPostComment newComment = new Areas.Blog.Models.BlogPostComment();
  196. newComment.BlogPostId = postMapping[postId];
  197. newComment.UserId = userMapping[userId];
  198. newComment.Article = comment["post"].ToString();
  199. newComment.DatePosted = DateTime.Parse(comment["date_posted"].ToString());
  200. newComment.DateEdited = DateTime.Parse(comment["date_posted"].ToString());
  201. context.BlogComments.AddOrUpdate(newComment);
  202. context.SaveChanges();
  203. }
  204. }
  205. // Transfer Pastes
  206. var pasteRet = db.Query("SELECT * FROM `paste`");
  207. foreach (var paste in pasteRet)
  208. {
  209. string pass = paste["password"].ToString();
  210. // If it's a password protected paste, we just skip it
  211. if (string.IsNullOrEmpty(pass) || pass == "EMPTY")
  212. {
  213. string content = paste["code"].ToString();
  214. string title = paste["title"].ToString();
  215. DateTime posted = DateTime.Parse(paste["posted"].ToString());
  216. int userId = Int32.Parse(paste["user_id"].ToString());
  217. Areas.Paste.Models.Paste newPaste = PasteHelper.CreatePaste(content, title);
  218. newPaste.DatePosted = posted;
  219. newPaste.Url = paste["pid"].ToString();
  220. if (userMapping.ContainsKey(userId) && userId != 0)
  221. {
  222. newPaste.UserId = userMapping[userId];
  223. }
  224. context.Pastes.AddOrUpdate(newPaste);
  225. context.SaveChanges();
  226. }
  227. }
  228. // Transfer Uploads
  229. var uploadRet = db.Query("SELECT * FROM `uploads`");
  230. foreach (var upload in uploadRet)
  231. {
  232. string url = upload["url"].ToString();
  233. Areas.Upload.Models.Upload upFound = context.Uploads.Where(u => u.Url == url).FirstOrDefault();
  234. if (upFound == null)
  235. {
  236. string fileType = upload["type"].ToString();
  237. int contentLength = Int32.Parse(upload["filesize"].ToString());
  238. string deleteKey = upload["delete_key"].ToString();
  239. int userId = Int32.Parse(upload["user_id"].ToString());
  240. DateTime uploadDate = DateTime.Parse(upload["upload_date"].ToString());
  241. string fullUrl = string.Format("https://u.teknik.io/{0}", url);
  242. string fileExt = Path.GetExtension(fullUrl);
  243. // Download the old file and re-upload it
  244. using (WebClient client = new WebClient())
  245. {
  246. try
  247. {
  248. byte[] fileData = client.DownloadData(fullUrl);
  249. // Generate key and iv if empty
  250. string key = Utility.RandomString(config.UploadConfig.KeySize / 8);
  251. string iv = Utility.RandomString(config.UploadConfig.BlockSize / 8);
  252. fileData = AES.Encrypt(fileData, key, iv);
  253. if (fileData == null || fileData.Length <= 0)
  254. {
  255. continue;
  256. }
  257. Areas.Upload.Models.Upload up = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, key, config.UploadConfig.KeySize, config.UploadConfig.BlockSize);
  258. if (userMapping.ContainsKey(userId))
  259. up.UserId = userMapping[userId];
  260. if (!string.IsNullOrEmpty(deleteKey))
  261. up.DeleteKey = deleteKey;
  262. up.Url = url;
  263. context.Uploads.Add(up);
  264. context.SaveChanges();
  265. }
  266. catch { }
  267. }
  268. }
  269. }
  270. }
  271. */
  272. }
  273. private void Db_MysqlErrorEvent(object sender, string e)
  274. {
  275. //throw new NotImplementedException();
  276. }
  277. }
  278. }