Beispiel #1
0
        public DtoActionResult AddFile(EntityUploadedFile file)
        {
            var actionResult = new DtoActionResult();

            file.DateUploaded = DateTime.UtcNow;

            var existingFile = _uow.UploadedFileRepository.GetFirstOrDefault(x => x.Name == file.Name && x.Guid == file.Guid);

            if (existingFile == null)
            {
                _uow.UploadedFileRepository.Insert(file);
                _uow.Save();
                actionResult.Success = true;
                actionResult.Id      = file.Id;
            }
            else
            {
                file.Id = existingFile.Id;
                _uow.UploadedFileRepository.Update(file, file.Id);
                _uow.Save();
                actionResult.Success = true;
                actionResult.Id      = file.Id;
            }

            return(actionResult);
        }
Beispiel #2
0
        public void ChunkingComplete()
        {
            var fileName = Request["qqfilename"];

            if (string.IsNullOrEmpty(fileName) || fileName == Path.DirectorySeparatorChar.ToString())
            {
                throw new HttpException();
            }
            var moduleGuid = Request["moduleGuid"];

            if (string.IsNullOrEmpty(moduleGuid))
            {
                throw new HttpException();
            }
            var fullPath = Path.Combine(_basePath, "software_uploads", moduleGuid, fileName);

            using (var unc = new UncServices())
            {
                if (unc.NetUseWithCredentials() || unc.LastError == 1219)
                {
                    var uploadedFile = new EntityUploadedFile();
                    uploadedFile.Name = fileName;
                    uploadedFile.Guid = moduleGuid;
                    uploadedFile.Hash = Utility.GetFileHash(fullPath);

                    var result = new ServiceUploadedFile().AddFile(uploadedFile);
                    if (!result.Success)
                    {
                        try
                        {
                            File.Delete(fullPath);
                        }
                        catch
                        {
                            //ignored
                        }
                        throw new HttpException();
                    }
                }
                else
                {
                    throw new HttpException("Could Not Reach Storage Path");
                }
            }
        }
Beispiel #3
0
        public bool DeleteModuleFile(EntityUploadedFile file)
        {
            if (file == null)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(file.Name) || file.Name == Path.DirectorySeparatorChar.ToString())
            {
                return(false);
            }
            if (string.IsNullOrEmpty(file.Guid) || file.Guid == Path.DirectorySeparatorChar.ToString())
            {
                return(false);
            }
            var basePath = Path.Combine(ServiceSetting.GetSettingValue(SettingStrings.StoragePath), "software_uploads");
            var fullPath = Path.Combine(basePath, file.Guid, file.Name);

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