示例#1
0
        public static DbFile UploadSingleFile(FileInfo fi, Crypto.EncryptionMode encryptionMode)
        {
            DbFile dbf = null;

            try
            {
                dbf    = new DbFile();
                dbf.Id = fi.FullName;
                dbf.DateLastWriteTime = fi.LastWriteTimeUtc;
                dbf.Name           = fi.Name;
                dbf.Tag            = null;
                dbf.Category       = null;
                dbf.EncryptionMode = encryptionMode;
                dbf.Size           = fi.Length;
                dbf.Checksum       = Checksum.Calculate(fi);
                if (UploadProcess(dbf) == false)
                {
                    Logger.Warn(LOGNAME, "File has not been uploaded");
                }
                else
                {
                    Logger.Info(LOGNAME, "Link: " + dbf.DownloadLink);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(LOGNAME, ex.Message, ex);
            }
            return(dbf);
        }
        /// <summary>
        /// Validate a password by checking the checksum.
        /// </summary>
        /// <param name="password">Password to validate.</param>
        /// <returns><c>true</c> if the password is valid, <c>false</c> otherwise.</returns>
        public static bool ValidatePassword(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            // Sanitaze and check password
            password = password.Replace(" ", "");
            password = password.Replace(Environment.NewLine, "");
            password = password.ToUpper();
            if (password.Length != PasswordLength)
            {
                throw new ArgumentException("Invalid password length");
            }

            // Do decryption rounds
            byte[] binary;
            try {
                password = Permutation.Decrypt(password, true);
                binary   = Substitution.Decrypt(password);
                Scramble.Decrypt(binary[0], binary, 1, binary.Length - 2);
            } catch {
                return(false);
            }

            // Validate checksum
            byte checksum    = binary[0];
            byte newChecksum = Checksum.Calculate(binary, 1, binary.Length - 1);

            return(checksum == newChecksum);
        }
示例#3
0
            public void Append(byte logType, byte[] key, byte[] value)
            {
                Debug.Assert(mBinaryWriter.BaseStream.Length == Length);

                try
                {
                    mBinaryWriter.Write(Checksum.Calculate(logType, key, value));
                    mBinaryWriter.Write(logType);
                    mBinaryWriter.Write(key.Length);
                    mBinaryWriter.Write(key);
                    mBinaryWriter.Write(value.Length);
                    mBinaryWriter.Write(value);
                    mBinaryWriter.Flush();

                    Length += sizeof(ulong) + 1 + sizeof(int) + key.Length + sizeof(int) + value.Length;
                }
                catch
                {
                    try
                    {
                        mBinaryWriter.Flush();
                        mBinaryWriter.BaseStream.SetLength(Length);
                    }
                    catch (Exception e)
                    {
                        throw new WriteAheadLogCorruptionException(
                                  string.Format("Found corrupted write ahead log [{0}], offset={1}", FilePath, Length), e);
                    }

                    throw;
                }
            }
示例#4
0
        public byte[] CreateTransmission(ulong laddress, ushort saddress, byte[] data, byte frame_id, byte radius, byte options)
        {
            ushort data_length  = (ushort)(14 + data.Length);
            ushort total_length = (ushort)(data_length + 4);

            byte[] result = new byte[total_length];
            //
            result[0]  = CONSTANTS.FRAME_START_BYTE;        // start byte
            result[1]  = (byte)(data_length >> 8);          // data length MSB
            result[2]  = (byte)data_length;                 // data length LSB
            result[3]  = CONSTANTS.FT_TRANSMIT;             // frame type
            result[4]  = frame_id;                          // frame ID
            result[5]  = (byte)(laddress >> 56);            // destination address MSB
            result[6]  = (byte)(laddress >> 48);            // destination address
            result[7]  = (byte)(laddress >> 40);            // destination address
            result[8]  = (byte)(laddress >> 32);            // destination address
            result[9]  = (byte)(laddress >> 24);            // destination address
            result[10] = (byte)(laddress >> 16);            // destination address
            result[11] = (byte)(laddress >> 8);             // destination address
            result[12] = (byte)laddress;                    // destination address LSB
            result[13] = (byte)(saddress >> 8);             // 16-bit address MSB
            result[14] = (byte)saddress;                    // 16-bit address LSB
            result[15] = radius;                            // broadcast radius
            result[16] = options;                           // transmission options
            for (int i = 0; i < data.Length; i++)
            {
                result[i + 17] = data[i];                                                  // data
            }
            byte checksum = Checksum.Calculate(result.Skip(3).Take(total_length - 3 - 1)); // skip first 3 bytes and last byte

            result[total_length - 1] = checksum;                                           // checksum
            return(result);
        }
        public byte[] CreateATCommand(string command, byte[] value, byte frame_id, bool queue)
        {
            throw new NotImplementedException("CreateATCommand not implemented for CustomFrameBytesFactory");
            ushort data_length  = (ushort)(4 + value.Length);
            ushort total_length = (ushort)(data_length + 4);

            byte[] result = new byte[total_length];
            //
            result[0] = CONSTANTS.FRAME_START_BYTE;               // start byte
            result[1] = (byte)(data_length >> 8);                 // data length MSB
            result[2] = (byte)data_length;                        // data length LSB
            result[3] = queue ?
                        CONSTANTS.FT_ATCMDQ : CONSTANTS.FT_ATCMD; // frame type
            result[4] = frame_id;                                 // frame ID
            result[5] = (byte)command[0];                         // AT command char 0
            result[6] = (byte)command[1];                         // AT command char 1
            for (int i = 0; i < value.Length; i++)
            {
                result[i + 7] = value[i];                                                  // data
            }
            byte checksum = Checksum.Calculate(result.Skip(3).Take(total_length - 3 - 1)); // skip first 3 bytes and last byte

            result[total_length - 1] = checksum;                                           // checksum
            return(result);
        }
示例#6
0
        public override void Draw(IBarCodeBuilder builder, string data, float x, float y)
        {
            // validate the supplement data
            Validate(data, 5);

            // calculate checksum to use in parity
            string parity = Checksum.Calculate(data);

            Draw(builder, data, parity, x, y);
        }
示例#7
0
 private string AppendChecksum(string coded)
 {
     if (Checksum == null)
     {
         Checksum = new Code128Checksum();
     }
     // append the checksum - note: the checksum won't appear in the text string
     coded += Checksum.Calculate(coded);
     return(coded);
 }
        /// <summary>
        /// Converts a password into mail information.
        /// </summary>
        /// <param name="password">The password to convert.</param>
        /// <returns>Mail information from the password.</returns>
        public static MissionMail Convert(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            // Sanitaze and check password
            password = password.Replace(" ", "");
            password = password.Replace(Environment.NewLine, "");
            password = password.ToUpper();
            if (password.Length != PasswordLength)
            {
                throw new ArgumentException("Invalid password length");
            }

            // Do decryption rounds
            // The last byte for "scramble" is ignored. It should be the null
            // terminator 0x00.
            password = Permutation.Decrypt(password, true);
            byte[] binary = Substitution.Decrypt(password);
            Scramble.Decrypt(binary[0], binary, 1, binary.Length - 2);

            // Validate checksum
            byte checksum    = binary[0]; // The scramble key is the checksum too.
            byte newChecksum = Checksum.Calculate(binary, 1, binary.Length - 1);

            if (checksum != newChecksum)
            {
                throw new FormatException("Invalid checksum");
            }

            // Convert the binary password into the structure.
            // Write the array into a stream to use the BitReader.
            DataStream stream = new DataStream();

            stream.Write(binary, 1, binary.Length - 1);
            BitReader reader = new BitReader(stream);

            MissionMail info = new MissionMail();

            info.Type           = (MissionState)reader.ReadByte(4);
            info.LocationId     = reader.ReadByte(7);
            info.FloorNumber    = reader.ReadByte(7);
            info.Random         = (info.Type == MissionState.Sos) ? reader.ReadUInt32(24) : 0x00;
            info.UID            = reader.ReadUInt64(64);
            info.ClientLanguage = (GameLanguage)reader.ReadByte(4);
            info.ClientName     = reader.ReadString(80, EncodingName);
            info.ObjectID1      = (info.Type == MissionState.Sos) ? (ushort)0x00 : reader.ReadUInt16(10);
            info.ObjectID2      = (info.Type == MissionState.Sos) ? (ushort)0x00 : reader.ReadUInt16(10);
            info.RescuerUID     = reader.ReadUInt64(64);
            info.GameType       = (GameType)reader.ReadByte(2);

            return(info);
        }
示例#9
0
        public void ChecksumTest()
        {
            // Arrange.
            // Data of a mock MODBUS packet (missing the checksum).
            byte[] data = new byte[] { 0x11, 0x03, 0x06, 0xAE, 0x41, 0x56, 0x52, 0x43, 0x40 };

            // Act.
            ushort crc = Checksum.Calculate(data);

            // Assert.
            Assert.AreEqual(0xAD49, crc);
        }
示例#10
0
 private string AppendChecksum(string data)
 {
     if (useChecksum)
     {
         if (Checksum == null)
         {
             Checksum = new Code39Checksum();
         }
         data += Checksum.Calculate(data);
     }
     return(data);
 }
示例#11
0
 private string AppendChecksum(string data)
 {
     // append the checksum if necessary
     if (useChecksum)
     {
         if (Checksum == null)
         {
             Checksum = new Modulo10Checksum();
         }
         data += Checksum.Calculate(data);
     }
     return(data);
 }
        /// <summary>
        /// Converts a missiong information into a password.
        /// </summary>
        /// <param name="info">Mission to convert.</param>
        /// <returns>The password.</returns>
        public static string Convert(MissionMail info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            // Serialize the structure into a bit stream
            DataStream stream = new DataStream();
            BitWriter  writer = new BitWriter(stream);

            writer.Write((byte)info.Type, 4);
            writer.Write(info.LocationId, 7);
            writer.Write(info.FloorNumber, 7);
            if (info.Type == MissionState.Sos)
            {
                writer.Write(info.Random, 24);
            }
            writer.Write(info.UID, 64);
            writer.Write((byte)info.ClientLanguage, 4);
            writer.Write(info.ClientName, 80, EncodingName);
            if (info.Type != MissionState.Sos)
            {
                writer.Write(info.ObjectID1, 10);
                writer.Write(info.ObjectID2, 10);
            }

            writer.Write(info.RescuerUID, 64);
            writer.Write((byte)info.GameType, 2);

            // Write the stream into an array for the rounds.
            // We allocate an extra space for the checksum (first byte)
            // and the null terminator (last byte).
            byte[] binary = new byte[stream.Length + 2];
            stream.Position = 0;
            stream.Read(binary, 1, binary.Length - 2);

            // Create checksum
            byte checksum = Checksum.Calculate(binary, 1, binary.Length - 1);

            binary[0] = checksum;

            // Do encryption rounds
            // The key is the checksum, we don't encrypt the null terminator.
            Scramble.Encrypt(checksum, binary, 1, binary.Length - 2);
            string password = Substitution.Encrypt(binary, PasswordLength);

            password = Permutation.Encrypt(password, true);

            return(password);
        }
示例#13
0
        public void Handle(Request request, Response response)
        {
            response.Chunked = true;

            string path = PartitionModule.Instance.JobBinaryPath;

            byte[] binary   = File.ReadAllBytes(path);
            string checksum = Checksum.Calculate(binary);

            response.AddHeader(ChecksumHeaderName, checksum);
            using (var body = response.Body)
            {
                body.Write(binary, 0, binary.Length);
            }
        }
示例#14
0
        /// <summary>
        /// Updates the factory and user section checksums.
        /// </summary>
        public void UpdateChecksums()
        {
            // update factory section checksum
            FactorySectionChecksum = ~Checksum.Calculate(Data, 0x34, 0x2C);
            if (Checksum.Calculate(Data, 0x30, 0x30) != 0xFFFFFFFF)
            {
                throw new InvalidDataException("This shouldn't happen, and the checksum algorithm must be wrong.");
            }

            // update user section checksum
            UserSectionChecksum = ~Checksum.Calculate(Data, 0x64, 0x5C);
            if (Checksum.Calculate(Data, 0x60, 0x60) != 0xFFFFFFFF)
            {
                throw new InvalidDataException("This shouldn't happen, and the checksum algorithm must be wrong.");
            }
        }
示例#15
0
        private string CreateMessage(CMDS command)
        {
            // Create a string builder, and the message starts with "$".
            StringBuilder message = new StringBuilder("$", 10);

            // Add the command.
            message.Append((char)command);

            // Calculate checksum and convert to string.
            string checksum = Checksum.Calculate(Encoding.ASCII.GetBytes(message.ToString())).ToString();

            // Add checksum to the message, preceeded by an asterisk.
            message.Append('*' + checksum + '\n');

            return(message.ToString());
        }
示例#16
0
        public unsafe void IcpmChecksum(byte[] IcpmPacket)
        {
            var input = new Span <byte>(IcpmPacket);

            Assert.True(IcmpV4.TryConsume(input, out var icmpIn, out var data));

            var checksum = icmpIn.HeaderChecksum;

            Assert.Equal(0, Checksum.Calculate(ref MemoryMarshal.GetReference(input), IcpmPacket.Length));

            icmpIn.HeaderChecksum = 0;
            var changedData = new Span <byte>(&icmpIn, Unsafe.SizeOf <IcmpV4>());

            changedData.CopyTo(input);

            var newChecksum = Checksum.Calculate(ref MemoryMarshal.GetReference(input), IcpmPacket.Length);

            Assert.Equal(checksum, newChecksum);
        }
示例#17
0
                public bool MoveNext()
                {
                    if (mBinaryReader.BaseStream.Position == mBinaryReader.BaseStream.Length)
                    {
                        return(false);
                    }

                    try
                    {
                        var position  = mBinaryReader.BaseStream.Position;
                        var checksum  = mBinaryReader.ReadUInt64();
                        var logType   = mBinaryReader.ReadByte();
                        var keyLength = mBinaryReader.ReadInt32();
                        var key       = mBinaryReader.ReadBytes(keyLength);
                        if (key.Length < keyLength)
                        {
                            throw new EndOfStreamException();
                        }
                        var valueLength = mBinaryReader.ReadInt32();
                        var value       = mBinaryReader.ReadBytes(valueLength);
                        if (value.Length < valueLength)
                        {
                            throw new EndOfStreamException();
                        }
                        if (checksum != Checksum.Calculate(logType, key, value))
                        {
                            throw new WriteAheadLogCorruptionException(
                                      string.Format("Found corrupted write ahead log [{0}], offset={1}", mFilePath, position));
                        }

                        mCurrent = Tuple.Create(logType, key, value);

                        return(true);
                    }
                    catch (EndOfStreamException)
                    {
                        mBinaryReader.BaseStream.Position = mBinaryReader.BaseStream.Length;
                        return(false);
                    }
                }
示例#18
0
        private bool ValidateChecksum(string message)
        {
            // Convert the message to a byte array.
            byte[] messageBytes = Encoding.ASCII.GetBytes(message);

            ushort rx_crc;
            ushort calc_crc;
            int    i     = 0;
            int    max_i = messageBytes.GetUpperBound(0) - 1;
            int    size  = 0;

            while (messageBytes[i++] != (byte)'*')
            {
                if (i >= max_i)
                {
                    return(false);
                }
            }

            size     = i - 1;
            calc_crc = Checksum.Calculate(messageBytes);
            string rx_crc_str = "";

            while (messageBytes[i] != (byte)'\n')
            {
                rx_crc_str = rx_crc_str + ((char)messageBytes[i++]).ToString();
            }
            rx_crc = ushort.Parse(rx_crc_str);
            if (rx_crc == calc_crc)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#19
0
        protected string Validate(string data, int fullLength)
        {
            // check for non digits
            if (!new Regex(@"^\d+$").IsMatch(data))
            {
                throw new BarCodeFormatException("The barcode has non-numeric data.");
            }

            // check the length, it can be full or missing the check digit
            if (data.Length < fullLength - 1 || data.Length > fullLength)
            {
                throw new BarCodeFormatException("Invalid length for barcode.");
            }

            // check or calculate and append the check digit
            if (Checksum == null)
            {
                Checksum = new Modulo10Checksum();
            }
            if (data.Length == fullLength)
            {
                // check if the check digit in the string is right
                string checksum = Checksum.Calculate(data.Substring(0, fullLength - 1));
                if (data[fullLength - 1].ToString() != checksum)
                {
                    throw new BarCodeFormatException("Invalid check digit.");
                }
            }
            else
            {
                // append the check digit
                data += Checksum.Calculate(data);
            }

            return(data);
        }
示例#20
0
文件: Checksum.cs 项目: InJoins/Magma
 public ushort GenerateChecksum() => Checksum.Calculate(ref _bytes[0], _bytes.Length);
示例#21
0
 public static string GetRowKey(EmailRequest email, EmailAction action)
 // user provided content. will contain non-allowed chars
 => Checksum.Calculate($"{email.Email.From.Email}_{email.Email.Date}_{action.Id}");
示例#22
0
        private string CreateMessage(CMDS command, SENSOR_ID sensor, uint ppm)
        {
            // Create a byte array.
            byte[] outbuff = new byte[30];

            // Keep track of the message length.
            int i = 0;

            // Message starts with "$".
            outbuff[i++] = (byte)'$';

            // Add the command.
            outbuff[i++] = (byte)command;

            // Add comma delimiter.
            outbuff[i++] = (byte)',';

            // Add the sensor ID.
            string str = ((int)sensor).ToString();

            for (int j = 0; j < str.Length; j++)
            {
                outbuff[i++] = (byte)str[j];
            }

            // Add comma delimiter.
            outbuff[i++] = (byte)',';

            // Add sensor data.
            str = ppm.ToString();
            for (int j = 0; j < str.Length; j++)
            {
                outbuff[i++] = (byte)str[j];
            }

            // Add comma delimiter.
            outbuff[i++] = (byte)',';

            // Add unit of measure.
            str = ((int)UNIT.PPM).ToString();
            for (int j = 0; j < str.Length; j++)
            {
                outbuff[i++] = (byte)str[j];
            }

            // Calculate checksum.
            ushort crc_value = Checksum.Calculate(outbuff);

            // Checksum is preceeded by an asterisk.
            outbuff[i++] = (byte)'*';

            // Add the checksum to the message.
            string crc_str      = crc_value.ToString();
            int    crc_str_size = crc_str.Length;

            for (int j = 0; j < crc_str_size; j++)
            {
                outbuff[i++] = (byte)crc_str[j];
            }

            // Terminate message with newline.
            outbuff[i++] = (byte)('\n');

            return(Encoding.ASCII.GetString(outbuff));
        }
示例#23
0
        public static bool DownloadProcess(DownloadLink dl, string outputdir)
        {
            try
            {
                if (dl.Version < MINIMAL_VERSION_SUPPORTED)
                {
                    Logger.Warn(LOGNAME, "This NzbLiteClient version doesn't support NzbLite format < " + MINIMAL_VERSION_SUPPORTED);
                    return(false);
                }
                DirectoryInfo workingDir = new DirectoryInfo(Utilities.FolderTemp);
                //On clean le workingdir
                FileInfo[] filesToRemove = workingDir.GetFiles();
                foreach (FileInfo fileToRemove in filesToRemove)
                {
                    Utilities.FileDelete(fileToRemove.FullName);
                }

                Stopwatch perfTotal    = new Stopwatch();
                Stopwatch perfPar      = new Stopwatch();
                Stopwatch perfDownload = new Stopwatch();

                Guid   usenetId    = dl.Id;
                byte[] encKey      = Crypto.GenerateEncryptionKey(usenetId, dl.EncryptionMode);
                string usenetIdStr = usenetId.ToString();

                Logger.Info(LOGNAME, "Start " + usenetIdStr);


                //1- Preparing Chunks for downloading Raw
                Logger.Info(LOGNAME, "Chunks " + usenetIdStr);

                long totalSize            = 0;
                DownloadLinkFileInfo dlfi = dl.DicoOfPassNumberPerExtension[Utilities.EXT_RAW];
                FileInfo             fi   = new FileInfo(Path.Combine(Utilities.FolderTemp, usenetIdStr));
                string filepath           = fi.FullName;

                BinaryWriter bw = new BinaryWriter(new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write, Utilities.BUFFER_SIZE));
                bw.BaseStream.SetLength(dlfi.Size);
                totalSize += dlfi.Size;
                int skippedChunks = 0;
                for (int i = 0; i < dlfi.ListOfPassNumber.Count; i++)
                {
                    if (dlfi.ListOfPassNumber[i] < UsenetServer.MAX_PASS)
                    {
                        UsenetChunk chunk = new UsenetChunk(bw, new FileInfo(filepath).Name, usenetId, Utilities.EXT_RAW, i, dlfi.ListOfPassNumber[i], dlfi.ListOfPassNumber.Count, dl.EncryptionMode);;
                        UsenetDownloader.AddChunk(chunk);
                    }
                    else
                    {
                        skippedChunks += 1;
                    }
                }

                //2- Run Download Tasks
                Logger.Info(LOGNAME, "Download starts " + usenetIdStr);
                perfDownload.Start();
                UsenetDownloader.Run(encKey, dl.Version);

                //3- Checking for end
                while (UsenetDownloader.IsFinished() == false)
                {
                    Task.Delay(50).Wait();
                }
                perfDownload.Stop();
                bw.Close();

                //4- Checksum for rawFile
                bool   success  = false;
                string checksum = Checksum.Calculate(fi);
                //checksum = "fakechecksum"; // DEBUG ONLY
                if (dl.Checksum == checksum) //File is OK
                {
                    success = true;
                }
                else
                {
                    //Downloading Parity Files
                    List <BinaryWriter> listOfBws = new List <BinaryWriter>();

                    foreach (KeyValuePair <string, DownloadLinkFileInfo> kvp in dl.DicoOfPassNumberPerExtension)
                    {
                        string extension = kvp.Key;
                        if (extension == Utilities.EXT_RAW)
                        {
                            continue;
                        }
                        filepath = fi.FullName + extension;

                        bw = new BinaryWriter(new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write, Utilities.BUFFER_SIZE));
                        bw.BaseStream.SetLength(kvp.Value.Size);
                        listOfBws.Add(bw);
                        totalSize += kvp.Value.Size;
                        int nbChunks = kvp.Value.ListOfPassNumber.Count;
                        for (int i = 0; i < nbChunks; i++)
                        {
                            if (kvp.Value.ListOfPassNumber[i] < UsenetServer.MAX_PASS)
                            {
                                UsenetChunk chunk = new UsenetChunk(bw, new FileInfo(filepath).Name, usenetId, extension, i, kvp.Value.ListOfPassNumber[i], nbChunks, dl.EncryptionMode);
                                UsenetDownloader.AddChunk(chunk);
                            }
                        }
                    }

                    //Run Download Tasks
                    Logger.Info(LOGNAME, "Download Par starts " + usenetIdStr);
                    perfDownload.Start();
                    UsenetDownloader.Run(encKey, dl.Version);

                    //Checking for end
                    while (UsenetDownloader.IsFinished() == false)
                    {
                        Task.Delay(50).Wait();
                    }
                    perfDownload.Stop();

                    //Free writers
                    foreach (BinaryWriter bww in listOfBws)
                    {
                        bww.Close();
                    }

                    //trying to repair file with PAR
                    perfPar.Start();
                    Logger.Info(LOGNAME, "Par2 " + usenetIdStr);
                    if (FilePariter.Repair(workingDir) == true)
                    {
                        checksum = Checksum.Calculate(fi);
                        if (dl.Checksum == checksum)
                        {
                            success = true;
                        }
                    }
                    perfPar.Stop();
                }

                //5- Moving to dest
                if (success == true)
                {
                    string   destFilepath = outputdir;
                    string[] tmp          = dl.Name.Split("/");
                    for (int i = 0; i < tmp.Length; i++)
                    {
                        destFilepath = Path.Combine(destFilepath, tmp[i]);
                        if (i < tmp.Length - 1)
                        {
                            Utilities.EnsureDirectory(destFilepath);
                        }
                        else
                        {
                            Utilities.FileDelete(destFilepath);
                        }
                    }
                    fi.MoveTo(destFilepath);
                }

                ////6- Process Finished
                //FileInfo[] filesToDelete = workingDir.GetFiles();
                //foreach (FileInfo fileToDelete in filesToDelete)
                //{
                //    Utilities.FileDelete(fileToDelete.FullName);
                //}
                perfTotal.Stop();

                if (success == true)
                {
                    Logger.Info(LOGNAME, "Success " + dl.Id + " (name: " + dl.Name + " - par: " + (long)(perfPar.ElapsedMilliseconds / 1000) + "s - down: " + (long)((perfDownload.ElapsedMilliseconds < 1000 ? 1000 : perfDownload.ElapsedMilliseconds) / 1000) + "s - speed: " + Utilities.ConvertSizeToHumanReadable(totalSize / (perfDownload.ElapsedMilliseconds / 1000)) + "b/s)");
                }
                else
                {
                    Logger.Info(LOGNAME, "Fail downloading " + dl.Id + " (name: " + dl.Name + ")");
                }
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error(LOGNAME, ex.Message, ex);
            }
            return(false);
        }
示例#24
0
        public static bool UploadProcess(DbFile dbf)
        {
            try
            {
                DirectoryInfo workingDir = new DirectoryInfo(Utilities.FolderTemp);
                //On clean le workingdir
                FileInfo[] filesToRemove = workingDir.GetFiles();
                foreach (FileInfo fileToRemove in filesToRemove)
                {
                    Utilities.FileDelete(fileToRemove.FullName);
                }

                Stopwatch perfTotal  = new Stopwatch();
                Stopwatch perfPar    = new Stopwatch();
                Stopwatch perfUpload = new Stopwatch();

                Logger.Info(LOGNAME, "[" + dbf.Id + "] Start");
                FileInfo fi = new FileInfo(dbf.Id);
                perfTotal.Start();

                Guid   usenetId     = Guid.NewGuid();
                byte[] encKey       = Crypto.GenerateEncryptionKey(usenetId, dbf.EncryptionMode);
                string usenetIdStr  = usenetId.ToString();
                string posterEmail  = Pokemon.GetEmail();
                string tempFilePath = Path.Combine(workingDir.FullName, usenetIdStr);

                //1- Copying file to temp
                fi.CopyTo(tempFilePath);

                //2- Checksum
                string checksum = Checksum.Calculate(new FileInfo(tempFilePath));
                dbf.Checksum = checksum;
                Logger.Info(LOGNAME, "[" + dbf.Id + "] Checksum: " + checksum);

                //3- Create Parity File
                perfPar.Start();
                Logger.Info(LOGNAME, "[" + dbf.Id + "] Par2");
                if (FilePariter.Create(workingDir, new FileInfo(tempFilePath), usenetIdStr + FilePariter.EXT_PAR2) == false)
                {
                    Logger.Info(LOGNAME, "Par2 error: " + dbf.Id);
                    return(false);
                }
                perfPar.Stop();

                //4- Preparing Chunks
                Logger.Info(LOGNAME, "[" + dbf.Id + "] Chunks");
                List <BinaryReader> listOfBrs           = new List <BinaryReader>();
                FileInfo[]          listOfFilesToUpload = workingDir.GetFiles();
                long totalSize = (from x in listOfFilesToUpload select x.Length).Sum();
                Dictionary <string, long> dicoOfSizePerExtension = new Dictionary <string, long>();
                Dictionary <string, List <UsenetChunk> > dicoOfChunksPerExtension = new Dictionary <string, List <UsenetChunk> >(); //key: extension - value: ListOfChunks
                foreach (FileInfo fileToUpload in listOfFilesToUpload)
                {
                    string extension = fileToUpload.Name.Replace(usenetIdStr, "");
                    if (string.IsNullOrEmpty(extension))
                    {
                        extension = Utilities.EXT_RAW;
                    }
                    dicoOfSizePerExtension[extension] = fileToUpload.Length;
                    int          nbChunks = (int)Math.Ceiling((double)fileToUpload.Length / Utilities.ARTICLE_SIZE);
                    BinaryReader br       = new BinaryReader(new FileStream(fileToUpload.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, Utilities.BUFFER_SIZE));
                    listOfBrs.Add(br);
                    dicoOfChunksPerExtension[extension] = new List <UsenetChunk>(nbChunks);
                    for (int i = 0; i < nbChunks; i++)
                    {
                        UsenetChunk chunk = new UsenetChunk(br, fileToUpload.Name, usenetId, extension, i, nbChunks, dbf.EncryptionMode);
                        dicoOfChunksPerExtension[extension].Add(chunk);
                        UsenetUploader.AddChunk(chunk);
                    }
                }

                //5- Run Upload Tasks
                Logger.Info(LOGNAME, "[" + dbf.Id + "] Upload");
                perfUpload.Start();
                UsenetUploader.Run(posterEmail, encKey);

                //6- Checking for end
                while (UsenetUploader.IsFinished() == false)
                {
                    Task.Delay(50).Wait();
                }
                perfUpload.Stop();

                //7- Process Finished
                foreach (BinaryReader br in listOfBrs)
                {
                    br.Close();
                }
                foreach (FileInfo fileToDelete in listOfFilesToUpload)
                {
                    Utilities.FileDelete(fileToDelete.FullName);
                }

                //8- Upload check
                List <UsenetChunk> mainChunkList = dicoOfChunksPerExtension[Utilities.EXT_RAW];
                int  nbSuccess     = (from x in mainChunkList where x.PassNumber != UsenetServer.MAX_PASS select x).Count();
                int  percSuccess   = (nbSuccess * 100) / mainChunkList.Count();
                bool uploadSuccess = true;
                if (percSuccess < Settings.Settings.Current.PercentSuccess)
                {
                    uploadSuccess = false;
                }

                if (uploadSuccess == true)
                {
                    //9- Generate DownloadLink
                    Dictionary <string, DownloadLinkFileInfo> dicoOfPassNumberPerExtension = new Dictionary <string, DownloadLinkFileInfo>(); //key: fileExtension|filesize - value: listOfPassNumber
                    foreach (KeyValuePair <string, List <UsenetChunk> > kvp in dicoOfChunksPerExtension)
                    {
                        string extension          = kvp.Key;
                        DownloadLinkFileInfo dlfi = new DownloadLinkFileInfo()
                        {
                            Size = dicoOfSizePerExtension[kvp.Key], ListOfPassNumber = (from x in kvp.Value orderby x.ChunkNumber ascending select x.PassNumber).ToList()
                        };
                        dicoOfPassNumberPerExtension[extension] = dlfi;
                    }

                    DownloadLink dl = new DownloadLink(DownloadLink.VERSION, usenetId, dbf.Name, dbf.Checksum, Utilities.UnixTimestampFromDate(DateTime.UtcNow), dbf.EncryptionMode, dicoOfPassNumberPerExtension);;
                    dbf.DownloadLink = DownloadLink.ToString(dl);
                }

                perfTotal.Stop();
                Logger.Info(LOGNAME, "[" + dbf.Id + "] " + (uploadSuccess == true ? "Success" : "Fail") + " (chunks: " + percSuccess + "% - files: " + listOfFilesToUpload.Length + " - size: " + Utilities.ConvertSizeToHumanReadable(totalSize) + " - par: " + (long)(perfPar.ElapsedMilliseconds / 1000) + "s - up: " + (long)(perfUpload.ElapsedMilliseconds / 1000) + "s - speed: " + Utilities.ConvertSizeToHumanReadable(totalSize / ((perfUpload.ElapsedMilliseconds < 1000 ? 1000 : perfUpload.ElapsedMilliseconds) / 1000)) + "b/s)");
                return(uploadSuccess);
            }
            catch (Exception ex)
            {
                Logger.Error(LOGNAME, ex.Message, ex);
            }
            return(false);
        }