예제 #1
0
        private static void AddUserGroupPermission(int userGroupID, int curentUserGroupPermissionID)
        {
            using (var ctx = new DFAppEntities())
            {
                var user       = ctx.UserGroups.Where(u => u.UserGroupID == userGroupID).FirstOrDefault();
                var permission = ctx.Permissions.Where(ug => ug.PermissionID == curentUserGroupPermissionID).FirstOrDefault();

                if (user != null && permission != null)
                {
                    UserGroupPermissions ugp = new UserGroupPermissions();
                    ugp.PermissionID = curentUserGroupPermissionID;
                    ugp.UserGroupID  = userGroupID;

                    user.UserGroupPermissions.Add(ugp);
                    ctx.SaveChanges();
                }
            }
        }
예제 #2
0
        static async Task NotificationHandler()
        {
            try
            {
                var token = notificationCancellation.Token;

                var fpns = new List <FilePublishedNotification>();

                while (token.IsCancellationRequested == false)
                {
                    fpns.Clear();
                    while (filesPublishedNotifications.IsEmpty == false)
                    {
                        if (filesPublishedNotifications.TryDequeue(out FilePublishedNotification fn))
                        {
                            fpns.Add(fn);
                        }
                        else
                        {
                            break;
                        }
                    }

                    var groupCounts = new Dictionary <string, int>();
                    foreach (var fpn in fpns)
                    {
                        if (groupCounts.TryGetValue(fpn.GroupId, out int count) == false)
                        {
                            groupCounts[fpn.GroupId] = 0;
                        }

                        groupCounts[fpn.GroupId] = groupCounts[fpn.GroupId] + 1;
                    }

                    foreach (var groupId in groupCounts.Keys)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        var maxCount = groupCounts[groupId];

                        UserGroupPermissions permissions = null;
                        if (groupPermissionLookup.TryGetValue(groupId, out permissions) == false)
                        {
                            continue;
                        }

                        var filesInfo = await client.GetDownloadableFilesInfoAsync(groupId, permissions.GroupOrganizerUserId, maxCount);

                        foreach (var fi in filesInfo)
                        {
                            // NOTE: if file is too large to download within the allowed timeout, might want to skip it
                            // if (fi.FileSize > MaxDownloadFileSize)
                            //    continue;

                            // download, decrypt, and uncompress file
                            Console.WriteLine("-------------------------------------");
                            Console.WriteLine($"Downloading {fi.FileName}, Decrypted Size {fi.FileSize} bytes");
                            await client.DownloadFileAsync(fi.FileId, fi.GroupId, async (downloadCipherTextStream) =>
                            {
                                string directory = null;
                                try
                                {
                                    directory = Path.Combine(downloadDirectory, fi.GroupId, fi.PublisherUserId);
                                    if (Directory.Exists(directory) == false)
                                    {
                                        Directory.CreateDirectory(directory);
                                    }

                                    directory = Path.Combine(decryptedDirectory, $"{fi.OrganizerEmail} - {fi.GroupName}", fi.PublisherEmail);
                                    if (Directory.Exists(directory) == false)
                                    {
                                        Directory.CreateDirectory(directory);
                                    }
                                }
                                catch (Exception dx)
                                {
                                    Console.WriteLine($"Error creating directories. Exception: {dx.Message}");
                                    return;
                                }

                                try
                                {
                                    var plainTextPath  = Path.Combine(directory, $"{fi.FileName}-{fi.FileId}{Path.GetExtension(fi.FileName)}");
                                    var cipherTextPath = Path.Combine(workingDirectory, $"{fi.FileId}.brx");
                                    var uncompressPath = Path.Combine(workingDirectory, $"{fi.FileId}.bru");

                                    FileMetadata metadata = null;
                                    byte[] md5            = null;
                                    using (var plainTextStream = File.Create(plainTextPath))
                                        using (var cipherTextStream = File.Create(cipherTextPath))
                                            using (var uncompressStream = File.Create(uncompressPath))
                                            {
                                                // download
                                                await downloadCipherTextStream.CopyToAsync(cipherTextStream);

                                                // decrypt and uncompress
                                                metadata = await client.UnprotectGroupFileAsync(cipherTextStream, uncompressStream, plainTextStream);

                                                plainTextStream.Position = 0;
                                                md5 = Client.ComputeMD5Hash(plainTextStream);
                                            }

                                    File.Delete(uncompressPath);
                                    File.Delete(cipherTextPath);

                                    if (md5.SequenceEqual(metadata.MD5Hash) == false)
                                    {
                                        Console.WriteLine($"WARNING: Possible corruption or tampering with file {plainTextPath} for group {fi.GroupId}");
                                    }
                                    else
                                    {
                                        Console.WriteLine($"Saved decrypted file {fi.FileName} to {plainTextPath}");
                                        Console.WriteLine();
                                    }
                                }
                                catch (Exception x)
                                {
                                    Console.WriteLine($"Error downloading file {fi.FileId} for group {fi.GroupId}. Exception: {x.Message}");
                                }
                            });
                        }
                    }

                    await Task.Delay(2000);
                }
            }
            catch (Exception nx)
            {
                Console.WriteLine($"Notification handler exception: {nx.Message} stack: {nx.StackTrace}");
            }
        }