public async Task <ActionResult <bool> > Exists(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(false); } if (!file.Permission.Info) { return(Forbid()); } return(System.IO.File.Exists(file.PhysicalPath)); }
public async Task <ActionResult> Download(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!file.Permission.Read) { return(Forbid()); } if (!System.IO.File.Exists(file.PhysicalPath)) { return(NotFound()); } string contentType = Utils.GetContentType(Path.GetExtension(file.Name)); return(PhysicalFile(file.PhysicalPath, contentType, file.Name)); }
public async Task <ActionResult> Copy(string encodedVirtualSrcPath, string encodedVirtualDestPath) { string virtualSrcPath = Utils.DecodePath(encodedVirtualSrcPath); if (virtualSrcPath == null) { return(BadRequest("Src encoding error")); } string virtualDestPath = Utils.DecodePath(encodedVirtualDestPath); if (virtualDestPath == null) { return(BadRequest("Dest encoding error")); } string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); InternalFile srcFile, destFile; try { srcFile = await ShareFileHelper.GetFileItem(virtualSrcPath, dbContext, userId, this); destFile = await ShareFileHelper.GetFileItem(virtualDestPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!srcFile.Permission.Write) { return(Forbid()); } if (!destFile.Permission.Write) { return(Forbid()); } try { await using Stream source = System.IO.File.OpenRead(srcFile.PhysicalPath); await using Stream destination = System.IO.File.Create(destFile.PhysicalPath); await source.CopyToAsync(destination); } catch (FileNotFoundException) { return(NotFound()); } return(Ok()); }
private async Task <InternalFile> ValidateAddFileShare(AddFileShareBody body) { await ValidateFileSystemItemShare(body); string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); InternalFile file = await ShareFileHelper.GetFileItem(body.Path, dbContext, userId, this); if (!HasPermission(file.Permission, body.Permission)) { throw (HttpResultException)Forbid(); } if (!System.IO.File.Exists(file.PhysicalPath)) { throw (HttpResultException)NotFound("File not found"); } return(file); }
public void WaitFileTest() { var docRoot = Path.Combine(Settings.Default.DocumentPath, "12345"); var waybillsPath = Path.Combine(docRoot, "Waybills"); Directory.CreateDirectory(waybillsPath); var filename = Path.Combine(waybillsPath, "14356_4.dbf"); File.Copy(@"..\..\Data\Waybills\14356_4.dbf", filename); bool exist = false; try { ShareFileHelper.WaitFile(filename); exist = true; } catch { } Assert.That(exist, Is.True); File.Delete(filename); try { ShareFileHelper.WaitFile(filename); } catch (Exception e) { Assert.That(e is WaitFileException, Is.True); Assert.That(e.Message, Is.EqualTo(String.Format("Файл {0} не появился в папке после 1000 мс ожидания.", filename))); } Thread thread = new Thread(() => { Thread.Sleep(2500); File.Copy(@"..\..\Data\Waybills\14356_4.dbf", filename); }); try { exist = false; thread.Start(); ShareFileHelper.WaitFile(filename, 5000); exist = true; } catch { } Assert.That(exist, Is.True); }
public async Task <ActionResult <FileItemInfo> > GetInfo(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!file.Permission.Info) { return(Forbid()); } try { FileInfo info = new FileInfo(file.PhysicalPath); if (!info.Exists) { return(NotFound()); } return(FileHelper.GetInfo(file, info)); } catch (FileNotFoundException) { return(NotFound()); } }
public async Task <ActionResult> Get(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!file.Permission.Read) { return(Forbid()); } if (!System.IO.File.Exists(file.PhysicalPath)) { return(NotFound()); } string fileName = Utils.ReplaceNonAscii(file.Name); Response.Headers.Add(HeaderNames.ContentDisposition, $"inline; filename=\"{fileName}\""); string contentType = Utils.GetContentType(Path.GetExtension(file.Name)); return(PhysicalFile(file.PhysicalPath, contentType, true)); }
public async Task <ActionResult <string> > GetHash(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!file.Permission.Hash) { return(Forbid()); } try { using HashAlgorithm hasher = SHA1.Create(); using FileStream stream = System.IO.File.OpenRead(file.PhysicalPath); return(Convert.ToBase64String(hasher.ComputeHash(stream))); } catch (FileNotFoundException) { return(NotFound()); } }
private static Document ProcessWaybill(DocumentReceiveLog log, string filename) { if (log.DocumentType == DocType.Reject) { return(null); } var settings = WaybillSettings.Find(log.ClientCode.Value); if (log.DocumentSize == 0) { return(null); } // ждем пока файл появится в удаленной директории if (!log.FileIsLocal()) { ShareFileHelper.WaitFile(filename, 5000); } var doc = SessionHelper.WithSession(s => { var detector = new WaybillFormatDetector(); var result = detector.Parse(s, filename, log); WaybillFormatDetector.Process(s, result); return(result); }); // для мульти файла, мы сохраняем в источнике все файлы, // а здесь, если нужна накладная в dbf формате, то сохраняем merge-файл в dbf формате. if (doc != null) { Exporter.ConvertIfNeeded(doc, settings); } return(doc); }
public async Task <ActionResult> Write(string encodedVirtualPath) { string virtualPath = Utils.DecodePath(encodedVirtualPath); if (virtualPath == null) { return(BadRequest("Path encoding error")); } InternalFile file; try { string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); file = await ShareFileHelper.GetFileItem(virtualPath, dbContext, userId, this); } catch (HttpResultException exc) { return(exc.Result); } if (!file.Permission.Write) { return(Forbid()); } string path = file.PhysicalPath; string tmpPath = FileHelper.GenerateUniqueFileName(path); try { byte[] buffer = new byte[100000]; await using (Stream src = Request.Body) await using (FileStream dest = System.IO.File.Create(tmpPath)) { int size; do { size = await src.ReadAsync(buffer, 0, buffer.Length); await dest.WriteAsync(buffer, 0, size); } while (size > 0); } if (tmpPath != path) { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } System.IO.File.Move(tmpPath, path); } } catch (Exception) { try { if (System.IO.File.Exists(tmpPath)) { System.IO.File.Delete(tmpPath); } } catch { } throw; } return(Ok()); }