Beispiel #1
0
        public void SaveLocal(string template, Dictionary <string, string> textFields, Dictionary <string, string> fileFields)
        {
            try
            {
                long embeddedStart = 0;

                string tempPath = Core.GetTempPath();
                byte[] key      = Utilities.GenerateKey(Core.StrongRndGen, 256);
                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    int written = 0;

                    // write template info
                    byte[] htmlBytes = UTF8Encoding.UTF8.GetBytes(template);
                    written += Protocol.WriteToFile(new ProfileAttachment("template", htmlBytes.Length), stream);


                    // write fields info (convert into fields into packet list)
                    List <byte[]> fieldPackets = new List <byte[]>();

                    int fieldsTotalSize = 0;

                    if (textFields != null)
                    {
                        foreach (KeyValuePair <string, string> pair in textFields)
                        {
                            if (pair.Value == null)
                            {
                                continue;
                            }

                            ProfileField field = new ProfileField();
                            field.Name      = pair.Key;
                            field.Value     = UTF8Encoding.UTF8.GetBytes(pair.Value);
                            field.FieldType = ProfileFieldType.Text;

                            byte[] packet = field.Encode(Network.Protocol);
                            fieldPackets.Add(packet);
                            fieldsTotalSize += packet.Length;
                        }
                    }

                    if (fileFields != null)
                    {
                        foreach (KeyValuePair <string, string> pair in fileFields)
                        {
                            if (pair.Value == null)
                            {
                                continue;
                            }

                            ProfileField field = new ProfileField();
                            field.Name      = pair.Key;
                            field.Value     = UTF8Encoding.UTF8.GetBytes(Path.GetFileName(pair.Value));
                            field.FieldType = ProfileFieldType.File;

                            byte[] packet = field.Encode(Network.Protocol);
                            fieldPackets.Add(packet);
                            fieldsTotalSize += packet.Length;
                        }
                    }

                    if (fieldsTotalSize > 0)
                    {
                        written += Protocol.WriteToFile(new ProfileAttachment("fields", fieldsTotalSize), stream);
                    }


                    // write files info
                    if (fileFields != null)
                    {
                        foreach (string path in fileFields.Values)
                        {
                            using (FileStream file = File.OpenRead(path))
                                written += Protocol.WriteToFile(new ProfileAttachment("file=" + Path.GetFileName(path), file.Length), stream);
                        }
                    }


                    stream.WriteByte(0); // end packets
                    embeddedStart = written + 1;

                    // write template bytes
                    stream.Write(htmlBytes, 0, htmlBytes.Length);

                    // write field bytes
                    foreach (byte[] packet in fieldPackets)
                    {
                        stream.Write(packet, 0, packet.Length);
                    }


                    // write file bytes
                    const int buffSize = 4096;
                    byte[]    buffer   = new byte[buffSize];

                    if (fileFields != null)
                    {
                        foreach (string path in fileFields.Values)
                        {
                            using (FileStream file = File.OpenRead(path))
                            {
                                int read = buffSize;
                                while (read == buffSize)
                                {
                                    read = file.Read(buffer, 0, buffSize);
                                    stream.Write(buffer, 0, read);
                                }
                            }
                        }
                    }

                    stream.FlushFinalBlock();
                }

                OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(embeddedStart));

                Store.PublishDirect(Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, vfile.SignedHeader);
            }
            catch (Exception ex)
            {
                Network.UpdateLog("Profile", "Error updating local " + ex.Message);
            }
        }
Beispiel #2
0
        public void SaveLocal(uint project)
        {
            try
            {
                string tempPath = Core.GetTempPath();
                byte[] key      = Utilities.GenerateKey(Core.StrongRndGen, 256);

                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    // write loaded projects
                    WorkingStorage working = null;
                    if (Working.ContainsKey(project))
                    {
                        working = Working[project];
                    }

                    if (working != null)
                    {
                        Protocol.WriteToFile(new StorageRoot(working.ProjectID), stream);
                        working.WriteWorkingFile(stream, working.RootFolder, true);

                        working.Modified = false;

                        try { File.Delete(GetWorkingPath(project)); }
                        catch { }
                    }

                    // open old file and copy entries, except for working
                    OpStorage local = GetStorage(Core.UserID);

                    if (local != null)
                    {
                        string oldPath = GetFilePath(local);

                        if (File.Exists(oldPath))
                        {
                            using (TaggedStream file = new TaggedStream(oldPath, Network.Protocol))
                                using (IVCryptoStream crypto = IVCryptoStream.Load(file, local.File.Header.FileKey))
                                {
                                    PacketStream oldStream = new PacketStream(crypto, Protocol, FileAccess.Read);
                                    bool         write     = false;
                                    G2Header     g2header  = null;

                                    while (oldStream.ReadPacket(ref g2header))
                                    {
                                        if (g2header.Name == StoragePacket.Root)
                                        {
                                            StorageRoot root = StorageRoot.Decode(g2header);

                                            write = (root.ProjectID != project);
                                        }

                                        //copy packet right to new file
                                        if (write) //crit test
                                        {
                                            stream.Write(g2header.Data, g2header.PacketPos, g2header.PacketSize);
                                        }
                                    }
                                }
                        }
                    }

                    stream.WriteByte(0); // signal last packet

                    stream.FlushFinalBlock();
                }

                SavingLocal = true; // prevents auto-integrate from re-calling saveLocal
                OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(Core.TimeNow.ToUniversalTime().ToBinary()));
                SavingLocal = false;

                Store.PublishDirect(Core.Trust.GetLocsAbove(), Core.UserID, ServiceID, FileTypeCache, vfile.SignedHeader);
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("Storage", "Error updating local " + ex.Message);
            }

            if (StorageUpdate != null)
            {
                Core.RunInGuiThread(StorageUpdate, GetStorage(Core.UserID));
            }
        }
Beispiel #3
0
        private void Cache_FileAquired(OpVersionedFile file)
        {
            OpPlan prevPlan = GetPlan(file.UserID, false);

            OpPlan newPlan = new OpPlan(file);

            PlanMap.SafeAdd(newPlan.UserID, newPlan);

            if (file.UserID == Core.UserID)
            {
                LocalPlan = newPlan;
            }

            if ((newPlan == LocalPlan) || (prevPlan != null && prevPlan.Loaded)) // if loaded, reload
            {
                LoadPlan(newPlan.UserID);
            }


            // update subs
            if (Network.Established)
            {
                List <LocationData> locations = new List <LocationData>();

                Trust.ProjectRoots.LockReading(delegate()
                {
                    foreach (uint project in Trust.ProjectRoots.Keys)
                    {
                        if (newPlan.UserID == Core.UserID || Trust.IsHigher(newPlan.UserID, project))
                        {
                            Trust.GetLocsBelow(Core.UserID, project, locations);
                        }
                    }
                });

                Store.PublishDirect(locations, newPlan.UserID, ServiceID, DataTypeFile, newPlan.File.SignedHeader);
            }


            // see if we need to update our own goal estimates
            if (newPlan.UserID != Core.UserID && LocalPlan != null)
            {
                Trust.ProjectRoots.LockReading(delegate()
                {
                    foreach (uint project in Trust.ProjectRoots.Keys)
                    {
                        if (Trust.IsLower(Core.UserID, newPlan.UserID, project)) // updated plan must be lower than us to have an effect
                        {
                            foreach (int ident in LocalPlan.GoalMap.Keys)
                            {
                                if (!newPlan.Loaded)
                                {
                                    LoadPlan(newPlan.UserID);
                                }

                                // if updated plan part of the same goal ident, re-estimate our own goals, incorporating update's changes
                                if (newPlan.GoalMap.ContainsKey(ident) || newPlan.ItemMap.ContainsKey(ident))
                                {
                                    foreach (PlanGoal goal in LocalPlan.GoalMap[ident])
                                    {
                                        int completed = 0, total = 0;

                                        GetEstimate(goal, ref completed, ref total);

                                        if (completed != goal.EstCompleted || total != goal.EstTotal)
                                        {
                                            goal.EstCompleted = completed;
                                            goal.EstTotal     = total;

                                            if (RunSaveLocal == 0) // if countdown not started, start
                                            {
                                                RunSaveLocal = SaveInterval;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }


            Core.RunInGuiThread(PlanUpdate, newPlan);

            if (Core.NewsWorthy(newPlan.UserID, 0, false))
            {
                Core.MakeNews(ServiceIDs.Plan, "Plan updated by " + Core.GetName(newPlan.UserID), newPlan.UserID, 0, false);
            }
        }