Exemple #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);
            }
        }