예제 #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);
            }
        }
예제 #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));
            }
        }
예제 #3
0
파일: PlanService.cs 프로젝트: nandub/DeOps
        public void SaveLocal()
        {
            try
            {
                string tempPath = Core.GetTempPath();
                byte[] key      = Utilities.GenerateKey(Core.StrongRndGen, 256);
                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    // write dummy block if nothing to write
                    OpPlan plan = GetPlan(Core.UserID, true);

                    if (plan == null ||
                        plan.Blocks == null ||
                        plan.Blocks.Count == 0)
                    {
                        Protocol.WriteToFile(new PlanBlock(), stream);
                    }


                    if (plan != null)
                    {
                        foreach (List <PlanBlock> list in plan.Blocks.Values)
                        {
                            foreach (PlanBlock block in list)
                            {
                                Protocol.WriteToFile(block, stream);
                            }
                        }

                        foreach (List <PlanGoal> list in plan.GoalMap.Values)
                        {
                            foreach (PlanGoal goal in list)
                            {
                                GetEstimate(goal, ref goal.EstCompleted, ref goal.EstTotal);
                                Protocol.WriteToFile(goal, stream);
                            }
                        }

                        foreach (List <PlanItem> list in plan.ItemMap.Values)
                        {
                            foreach (PlanItem item in list)
                            {
                                Protocol.WriteToFile(item, stream);
                            }
                        }
                    }

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

                    stream.FlushFinalBlock();
                }

                OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null);

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