Exemplo n.º 1
0
        public string GetMulticastFileNameWithFullPath(string imageName, string schemaHdNumber, string partitionNumber,
                                                       string extension, string basePath)
        {
            var filePath = "";

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    var imagePath = basePath + "images" +
                                    Path.DirectorySeparatorChar + imageName + Path.DirectorySeparatorChar + "hd" +
                                    schemaHdNumber;
                    try
                    {
                        filePath =
                            Directory.GetFiles(
                                imagePath + Path.DirectorySeparatorChar, "part" + partitionNumber + "." + extension + ".*")
                            .FirstOrDefault();
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    return("N/A");
                }
            }


            return(filePath);
        }
Exemplo n.º 2
0
 private string CreateDirectory()
 {
     using (var unc = new UncServices())
     {
         if (unc.NetUseWithCredentials() || unc.LastError == 1219)
         {
             var directory = new DirectoryInfo(_upload.DestinationDirectory);
             try
             {
                 if (!directory.Exists)
                 {
                     directory.Create();
                 }
                 return(null);
             }
             catch (Exception ex)
             {
                 return(ex.Message);
             }
         }
         else
         {
             return("Could Not Reach Storage Path");
         }
     }
 }
Exemplo n.º 3
0
        public string ReadSchemaFile(string imageName)
        {
            var schemaText = "";
            var basePath   = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);
            var path       = basePath + "images" + Path.DirectorySeparatorChar +
                             imageName + Path.DirectorySeparatorChar + "schema";

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        using (var reader = new StreamReader(path))
                        {
                            schemaText = reader.ReadLine() ?? "";
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Could Not Read Schema File.");
                        log.Error(ex.Message);
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    return(null);
                }
            }

            return(schemaText);
        }
Exemplo n.º 4
0
        public string GetHdFileSize(string imageName, string hd)
        {
            var basePath = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        var imagePath = basePath + "images" + Path.DirectorySeparatorChar + imageName +
                                        Path.DirectorySeparatorChar + "hd" + hd;
                        var size = GetDirectorySize(new DirectoryInfo(imagePath)) / 1024f / 1024f / 1024f;
                        return(Math.Abs(size) < 0.1f ? "< 100M" : size.ToString("#.##") + " GB");
                    }
                    catch
                    {
                        return("N/A");
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    return("N/A");
                }
            }
        }
Exemplo n.º 5
0
        public bool DeleteImageFolders(string imageName)
        {
            //Check again
            if (string.IsNullOrEmpty(imageName))
            {
                return(false);
            }

            var basePath = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        Directory.Delete(basePath + @"\images" + @"\" +
                                         imageName, true);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                        return(false);
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    return(false);
                }
            }
        }
Exemplo n.º 6
0
        public bool RenameImageFolder(string oldName, string newName)
        {
            //Check again
            if (string.IsNullOrEmpty(oldName) || string.IsNullOrEmpty(newName))
            {
                return(false);
            }

            var basePath = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        var imagePath = basePath + @"\images\";
                        Directory.Move(imagePath + oldName, imagePath + newName);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                        return(false);
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    return(false);
                }
            }
        }
Exemplo n.º 7
0
        public bool DeleteModuleDirectory(string moduleGuid)
        {
            if (string.IsNullOrEmpty(moduleGuid) || moduleGuid == Path.DirectorySeparatorChar.ToString())
            {
                return(false);
            }
            var basePath = Path.Combine(ServiceSetting.GetSettingValue(SettingStrings.StoragePath), "software_uploads");
            var fullPath = Path.Combine(basePath, moduleGuid);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    if (!Directory.Exists(fullPath))
                    {
                        return(true);
                    }
                    try
                    {
                        Directory.Delete(fullPath, true);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                        return(false);
                    }
                }
                else
                {
                    log.Error("Could Not Reach Storage Path");
                    return(false);
                }
            }
        }
Exemplo n.º 8
0
        private void CalculateMd5()
        {
            EntityDownload.Status = EnumFileDownloader.DownloadStatus.CalculatingMd5;
            _serviceExternalDownload.Update(EntityDownload);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        _hasher = new ServiceFileHash(MD5.Create());
                        _hasher.FileHashingProgress += OnFileHashingProgress;
                        using (var stream = new BufferedStream(File.OpenRead(Path.Combine(_destinationDir, EntityDownload.FileName)), 1200000))
                            _hasher.ComputeHash(stream);
                        EntityDownload.Md5Hash = _hasher.ToString();
                        EntityDownload.Status  = EnumFileDownloader.DownloadStatus.Complete;
                    }
                    catch (Exception ex)
                    {
                        EntityDownload.Status       = EnumFileDownloader.DownloadStatus.Error;
                        EntityDownload.ErrorMessage = ex.Message;
                    }
                }
            }

            _serviceExternalDownload.Update(EntityDownload);
        }
Exemplo n.º 9
0
        private string SaveBlobAlternate()
        {
            var path = Path.Combine(_upload.DestinationDirectory, _upload.PartUuid + "." + _upload.PartIndex);

            SaveAs(path);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    if (_upload.PartIndex == (_upload.TotalParts - 1))
                    {
                        ulong bytesWritten = 0;
                        using (
                            var output =
                                System.IO.File.OpenWrite(Path.Combine(_upload.DestinationDirectory,
                                                                      _upload.OriginalFilename))
                            )
                        {
                            for (var i = 0; i < _upload.TotalParts; i++)
                            {
                                using (
                                    var input =
                                        System.IO.File.OpenRead(Path.Combine(_upload.DestinationDirectory,
                                                                             _upload.PartUuid + "." + i))
                                    )
                                {
                                    var buff = new byte[1];
                                    while (input.Read(buff, 0, 1) > 0)
                                    {
                                        output.WriteByte(buff[0]);
                                        bytesWritten++;
                                    }
                                    input.Close();
                                }
                                output.Flush();
                            }
                            output.Close();

                            if (bytesWritten != _upload.FileSize)
                            {
                                return("Filesize Mismatch");
                            }

                            for (var i = 0; i < _upload.TotalParts; i++)
                            {
                                System.IO.File.Delete(Path.Combine(_upload.DestinationDirectory,
                                                                   _upload.PartUuid + "." + i));
                            }
                        }
                    }
                }
                else
                {
                    return("Could Not Reach Storage Path");
                }
            }
            return(null);
        }
Exemplo n.º 10
0
        private string SaveBlob()
        {
            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    var filePath = Path.Combine(_upload.DestinationDirectory, _upload.OriginalFilename);
                    if (_upload.PartIndex == 0)
                    {
                        var createDirResult = CreateDirectory();
                        if (createDirResult != null)
                        {
                            return(createDirResult);
                        }

                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                    }

                    Stream stream = null;
                    try
                    {
                        stream = new FileStream(filePath, (_upload.PartIndex == 0) ? FileMode.Create : FileMode.Append);
                        _upload.InputStream.CopyTo(stream, 16384);
                    }
                    catch (Exception ex)
                    {
                        return(ex.Message);
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Dispose();
                        }
                    }
                }
                else
                {
                    return("Could Not Reach Storage Path");
                }
            }


            return(null);
        }
Exemplo n.º 11
0
        public DtoFreeSpace GetSMBFreeSpace()
        {
            var storageType = ServiceSetting.GetSettingValue(SettingStrings.StorageType);

            if (storageType.Equals("Local"))
            {
                return(null);                             //no smb share setup
            }
            var dpFreeSpace = new DtoFreeSpace();
            var basePath    = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    ulong freespace = 0;
                    ulong total     = 0;
                    var   success   = DriveFreeBytes(basePath, out freespace, out total);

                    if (!success)
                    {
                        return(null);
                    }

                    var freePercent = 0;
                    var usedPercent = 0;

                    if (total > 0 && freespace > 0)
                    {
                        freePercent = (int)(0.5f + 100f * Convert.ToInt64(freespace) / Convert.ToInt64(total));
                        usedPercent =
                            (int)(0.5f + 100f * Convert.ToInt64(total - freespace) / Convert.ToInt64(total));
                    }
                    dpFreeSpace.freespace   = freespace;
                    dpFreeSpace.total       = total;
                    dpFreeSpace.freePercent = freePercent;
                    dpFreeSpace.usedPercent = usedPercent;
                    dpFreeSpace.dPPath      = basePath;
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                }
            }

            return(dpFreeSpace);
        }
Exemplo n.º 12
0
        public List <DtoImageFileInfo> GetPartitionFileSize(string imageName, string hd, string partition)
        {
            var basePath      = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);
            var imageFileInfo = new DtoImageFileInfo();

            using (var unc = new UncServices())
            {
                if (
                    unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        var imageFile =
                            Directory.GetFiles(
                                basePath + "images" + Path.DirectorySeparatorChar + imageName +
                                Path.DirectorySeparatorChar + "hd" + hd +
                                Path.DirectorySeparatorChar,
                                "part" + partition + ".*").FirstOrDefault();

                        var fi = new FileInfo(imageFile);
                        imageFileInfo = new DtoImageFileInfo
                        {
                            FileName = fi.Name,
                            FileSize = (fi.Length / 1024f / 1024f).ToString("#.##") + " MB"
                        };
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                        return(null);
                    }
                }
                else
                {
                    log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                }
            }

            return(new List <DtoImageFileInfo> {
                imageFileInfo
            });
        }
Exemplo n.º 13
0
        public bool DeleteExternalFile(EntityExternalDownload file)
        {
            if (file == null)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(file.FileName) || file.FileName == Path.DirectorySeparatorChar.ToString())
            {
                return(false);
            }
            if (string.IsNullOrEmpty(file.ModuleGuid) || file.ModuleGuid == Path.DirectorySeparatorChar.ToString())
            {
                return(false);
            }
            var basePath = Path.Combine(ServiceSetting.GetSettingValue(SettingStrings.StoragePath), "software_uploads");
            var fullPath = Path.Combine(basePath, file.ModuleGuid, file.FileName);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        File.Delete(fullPath);
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Could Not Delete " + fullPath);
                        log.Error(ex.Message);
                        return(false);
                    }
                }
                else
                {
                    log.Error("Could Not Reach Storage Path");
                    return(false);
                }
            }
        }
Exemplo n.º 14
0
        public async Task DownloadFile()
        {
            EntityDownload.DateDownloaded = DateTime.Now;
            EntityDownload.Status         = EnumFileDownloader.DownloadStatus.Downloading;

            if (EntityDownload.Id == 0)
            {
                _serviceExternalDownload.Add(EntityDownload);
            }
            else
            {
                _serviceExternalDownload.Update(EntityDownload);
            }

            _destinationDir = Path.Combine(ServiceSetting.GetSettingValue(SettingStrings.StoragePath), "software_uploads", EntityDownload.ModuleGuid);
            var dirResult = CreateDirectory();

            if (dirResult != null)
            {
                EntityDownload.Status       = EnumFileDownloader.DownloadStatus.Error;
                EntityDownload.ErrorMessage = dirResult;
                _serviceExternalDownload.Update(EntityDownload);
                return;
            }

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    using (_webClient = new WebClient())
                    {
                        _webClient.DownloadProgressChanged += wc_DownloadProgressChanged;
                        _webClient.DownloadFileCompleted   += wc_DownloadFileCompleted;
                        await _webClient.DownloadFileTaskAsync(new Uri(EntityDownload.Url), Path.Combine(_destinationDir, EntityDownload.FileName));
                    }
                }
            }
        }
Exemplo n.º 15
0
        private string SaveAs(string type)
        {
            var filePath = Path.Combine(_upload.DestinationDirectory, _upload.Filename);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    try
                    {
                        using (var file = new FileStream(filePath, FileMode.Create))
                            _upload.InputStream.CopyTo(file);
                    }
                    catch (Exception ex)
                    {
                        return(ex.Message);
                    }

                    if (type.Equals("module"))
                    {
                        var uploadedFile = new EntityUploadedFile();
                        uploadedFile.Name = _upload.Filename;
                        uploadedFile.Guid = _upload.ModuleGuid;
                        uploadedFile.Hash = Utility.GetFileHash(filePath);

                        var result = new ServiceUploadedFile().AddFile(uploadedFile);
                        if (!result.Success)
                        {
                            try
                            {
                                File.Delete(filePath);
                            }
                            catch
                            {
                                //ignored
                            }

                            return("Could Not Update Database");
                        }
                    }
                    else if (type.Equals("attachment"))
                    {
                        var attachment = new EntityAttachment();
                        attachment.AttachmentTime = DateTime.Now;
                        attachment.DirectoryGuid  = _upload.AttachmentGuid;
                        attachment.Name           = _upload.Filename;
                        attachment.UserName       = _upload.Username;
                        var result = new ServiceAttachment().Add(attachment);
                        if (!result.Success)
                        {
                            throw new HttpException();
                        }

                        if (_upload.AssetId != null)
                        {
                            var asset = new EntityAssetAttachment();
                            asset.AssetId      = Convert.ToInt32(_upload.AssetId);
                            asset.AttachmentId = attachment.Id;
                            result             = new ServiceAssetAttachment().Add(asset);
                            if (!result.Success)
                            {
                                throw new HttpException();
                            }
                        }

                        if (_upload.ComputerId != null)
                        {
                            var computer = new EntityComputerAttachment();
                            computer.ComputerId   = Convert.ToInt32(_upload.ComputerId);
                            computer.AttachmentId = attachment.Id;
                            result = new ServiceComputerAttachment().Add(computer);
                            if (!result.Success)
                            {
                                throw new HttpException();
                            }
                        }
                    }
                }
                else
                {
                    return("Could Not Reach Storage Path");
                }
            }
            return(null);
        }
Exemplo n.º 16
0
        public DtoFreeSpace GetStorageFreeSpace(bool isRemote)
        {
            var storageType = ServiceSetting.GetSettingValue(SettingStrings.StorageType);

            if (storageType.Equals("Local") && isRemote)
            {
                return(null);                                         //no remote share setup
            }
            var dpFreeSpace = new DtoFreeSpace();

            if (isRemote)
            {
                var basePath = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);
                var username = ServiceSetting.GetSettingValue(SettingStrings.StorageUsername);
                var domain   = ServiceSetting.GetSettingValue(SettingStrings.StorageDomain);
                var password = ServiceSetting.GetSettingValue(SettingStrings.StoragePassword);
                dpFreeSpace.dPPath = basePath;
                using (var unc = new UncServices())
                {
                    var smbPassword = new EncryptionServices().DecryptText(password);
                    if (
                        unc.NetUseWithCredentials(basePath, username, domain,
                                                  smbPassword) || unc.LastError == 1219)
                    {
                        ulong freespace = 0;
                        ulong total     = 0;
                        var   success   = DriveFreeBytes(basePath, out freespace, out total);

                        if (!success)
                        {
                            return(null);
                        }

                        var freePercent = 0;
                        var usedPercent = 0;

                        if (total > 0 && freespace > 0)
                        {
                            freePercent = (int)(0.5f + 100f * Convert.ToInt64(freespace) / Convert.ToInt64(total));
                            usedPercent =
                                (int)(0.5f + 100f * Convert.ToInt64(total - freespace) / Convert.ToInt64(total));
                        }
                        dpFreeSpace.freespace   = freespace;
                        dpFreeSpace.total       = total;
                        dpFreeSpace.freePercent = freePercent;
                        dpFreeSpace.usedPercent = usedPercent;
                    }
                    else
                    {
                        log.Error("Failed to connect to " + basePath + "\r\nLastError = " + unc.LastError);
                    }
                }
            }
            else
            {
                string path;
                if (storageType.Equals("Local"))
                {
                    path = ServiceSetting.GetSettingValue(SettingStrings.StoragePath);
                }
                else
                {
                    path = ConfigurationManager.AppSettings["LocalStoragePath"];
                }
                dpFreeSpace.dPPath = path;

                if (Directory.Exists(path))
                {
                    ulong freespace = 0;
                    ulong total     = 0;


                    bool success;

                    success = DriveFreeBytes(path, out freespace, out total);



                    if (!success)
                    {
                        return(null);
                    }

                    var freePercent = 0;
                    var usedPercent = 0;

                    if (total > 0 && freespace > 0)
                    {
                        freePercent = (int)(0.5f + 100f * Convert.ToInt64(freespace) / Convert.ToInt64(total));
                        usedPercent =
                            (int)(0.5f + 100f * Convert.ToInt64(total - freespace) / Convert.ToInt64(total));
                    }
                    dpFreeSpace.freespace   = freespace;
                    dpFreeSpace.total       = total;
                    dpFreeSpace.freePercent = freePercent;
                    dpFreeSpace.usedPercent = usedPercent;
                }
            }

            return(dpFreeSpace);
        }