Exemplo n.º 1
0
        public IHttpActionResult EditFile(string token, int id, int UserId, JDE_Files item)
        {
            if (token != null && token.Length > 0)
            {
                var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim());
                if (tenants.Any())
                {
                    var items = db.JDE_Files.AsNoTracking().Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.FileId == id);
                    if (items.Any())
                    {
                        JDE_Logs Log = new JDE_Logs {
                            UserId = UserId, Description = "Edycja pliku", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, OldValue = new JavaScriptSerializer().Serialize(items.FirstOrDefault()), NewValue = new JavaScriptSerializer().Serialize(item)
                        };
                        db.JDE_Logs.Add(Log);
                        db.Entry(item).State = EntityState.Modified;
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                            if (!JDE_FileExists(id))
                            {
                                return(NotFound());
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 2
0
        public IHttpActionResult DeleteFile(string token, int id, int UserId, int?PlaceId = null, int?PartId = null, int?ProcessId = null, int?UserLogId = null)
        {
            //File should only be deleted if we're deleting last fileAssign
            //otherwise we're deleting only fileAssign

            try
            {
                if (token != null && token.Length > 0)
                {
                    var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim());
                    if (tenants.Any())
                    {
                        var items = db.JDE_Files.Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.FileId == id);
                        if (items.Any())
                        {
                            JDE_Files item = items.FirstOrDefault();
                            if (PlaceId == null && PartId == null && ProcessId == null && UserLogId == null)
                            {
                                //well, we don't care for fileAssigns whatsover, just delete the file and all assigns pointing to it
                                //such a request must come from uploadKeeper
                                _DeleteFile(items.FirstOrDefault().Token, items.FirstOrDefault().Type, UserId, (int)items.FirstOrDefault().TenantId);
                            }
                            else
                            {
                                var fileAssigns = db.JDE_FileAssigns.Where(fi => fi.FileId == id);
                                if (fileAssigns.Any())
                                {
                                    //at least 1 fileAssign
                                    //otherwise it would be weird
                                    _DeleteFileAssigns(id, UserId, (int)fileAssigns.FirstOrDefault().TenantId, PlaceId, PartId, ProcessId, UserLogId);
                                    if (fileAssigns.Count() == 1)
                                    {
                                        //there's only 1 assigns so more it's the last one
                                        _DeleteFile(items.FirstOrDefault().Token, items.FirstOrDefault().Type, UserId, (int)items.FirstOrDefault().TenantId);
                                    }
                                }
                                else
                                {
                                    //there's no fileAssign
                                    //existing file should be deleted
                                    //should never happen in the first place
                                    _DeleteFile(items.FirstOrDefault().Token, items.FirstOrDefault().Type, UserId, (int)items.FirstOrDefault().TenantId);
                                }
                            }

                            return(Ok());
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Błąd podczas próby usuwania pliku {FileId}. Szczegóły: {details}", id, ex);
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage UploadFile(string token, string fileToken)
        {
            if (token != null && token.Length > 0)
            {
                Logger.Info("UploadFile: Start. fileToken={fileToken}", fileToken);
                var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim());
                if (tenants.Any())
                {
                    var httpRequest = HttpContext.Current.Request;

                    if (httpRequest.ContentLength > 0)
                    {
                        if (httpRequest.ContentLength > Static.RuntimeSettings.MaxFileContentLength)
                        {
                            Logger.Warn("Uploadfile: Plik przekracza dopuszczalną wielkość {limit} MB dla JDE_Files={token}.", Static.RuntimeSettings.MaxFileContentLength, fileToken);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, $"Przekroczono dopuszczalną wielość pliku ({Static.RuntimeSettings.MaxFileContentLength} MB)"));
                        }

                        var    postedFile = httpRequest.Files[0];
                        string filePath   = "";
                        if (postedFile != null && postedFile.ContentLength > 0)
                        {
                            try
                            {
                                var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));

                                filePath = $"{Static.RuntimeSettings.Path2Files}{fileToken + ext.ToLower()}";

                                postedFile.SaveAs(filePath);
                                if (postedFile.IsImage())
                                {
                                    Static.Utilities.ProduceThumbnail(filePath);
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("UploadFile: Błąd podczas próby zapisania pliku w {path} JDE_Files={token}. Szczegóły: {details}", filePath, fileToken, ex);
                                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
                            }
                            //edit file data to mark IsUploaded as true

                            try
                            {
                                JDE_Files file = db.JDE_Files.Where(f => f.Token == fileToken).FirstOrDefault();
                                if (file != null)
                                {
                                    file.IsUploaded      = true;
                                    file.Link            = RuntimeSettings.Path2Files;
                                    db.Entry(file).State = EntityState.Modified;
                                    db.SaveChanges();
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("UploadFile: Błąd podczas próby edycji JDE_Files={token}. Szczegóły: {details}", fileToken, ex);
                                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
                            }

                            return(Request.CreateResponse(HttpStatusCode.OK));
                        }
                        else
                        {
                            Logger.Warn("Uploadfile: Brak pliku dla JDE_Files={token}.", fileToken);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Brak pliku"));
                        }
                    }
                    else
                    {
                        Logger.Warn("Uploadfile: Próba wysłania pustego pliku dla JDE_Files={token}.", fileToken);
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Plik nieprawidłowy (plik zawiera 0 bajtów)"));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }
Exemplo n.º 4
0
        public IHttpActionResult CreateFile(string token, JDE_Files item, int UserId, int?PlaceId = null, int?PartId = null, int?ProcessId = null, int?UserLogId = null)
        {
            if (token != null && token.Length > 0)
            {
                var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim());
                if (tenants.Any())
                {
                    try
                    {
                        item.TenantId  = tenants.FirstOrDefault().TenantId;
                        item.CreatedOn = DateTime.Now;
                        item.CreatedBy = UserId;
                        item.Token     = Static.Utilities.GetToken();
                        db.JDE_Files.Add(item);
                        db.SaveChanges();
                        JDE_Logs Log = new JDE_Logs {
                            UserId = UserId, Description = "Utworzenie pliku", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item)
                        };
                        db.JDE_Logs.Add(Log);
                        db.SaveChanges();

                        JDE_FileAssigns FileAssing = new JDE_FileAssigns {
                            FileId = item.FileId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId, PartId = PartId, PlaceId = PlaceId, ProcessId = ProcessId, UserLogId = UserLogId
                        };
                        string word = "";
                        if (PlaceId != null)
                        {
                            word = "zasobu";
                        }
                        else if (PartId != null)
                        {
                            word = "części";
                        }
                        else if (ProcessId != null)
                        {
                            word = "zgłoszenia";
                        }
                        else
                        {
                            word = "logu użytkownika";
                        }
                        db.JDE_FileAssigns.Add(FileAssing);
                        db.SaveChanges();
                        JDE_Logs Log2 = new JDE_Logs {
                            UserId = UserId, Description = $"Przypisanie pliku do {word}", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item)
                        };
                        db.JDE_Logs.Add(Log2);
                        db.SaveChanges();
                        return(Ok(item));
                    }catch (Exception ex)
                    {
                        return(InternalServerError(ex));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                return(NotFound());
            }
        }
Exemplo n.º 5
0
        public HttpResponseMessage CreateFile(string token, string FileJson, int UserId, int?PlaceId = null, int?PartId = null, int?ProcessId = null, int?UserLogId = null)
        {
            if (token != null && token.Length > 0)
            {
                var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim());
                if (tenants.Any())
                {
                    JavaScriptSerializer jss  = new JavaScriptSerializer();
                    JDE_Files            item = jss.Deserialize <JDE_Files>(FileJson);
                    var httpRequest           = HttpContext.Current.Request;

                    if (httpRequest.ContentLength > 0)
                    {
                        if (httpRequest.ContentLength > Static.RuntimeSettings.MaxFileContentLength)
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, $"{item.Name} przekracza dopuszczalną wielość pliku ({Static.RuntimeSettings.MaxFileContentLength} MB) i został odrzucony"));
                        }
                        if (string.IsNullOrEmpty(item.Token))
                        {
                            //create unique token unless the file already exists
                            item.Token    = Static.Utilities.GetToken();
                            item.TenantId = tenants.FirstOrDefault().TenantId;
                            var    postedFile = httpRequest.Files[0];
                            string filePath   = "";
                            if (postedFile != null && postedFile.ContentLength > 0)
                            {
                                var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));

                                filePath = $"{Static.RuntimeSettings.Path2Files}{item.Token +  ext.ToLower()}";

                                postedFile.SaveAs(filePath);
                                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item);
                            }
                            item.CreatedOn = DateTime.Now;
                            item.CreatedBy = UserId;
                            item.Link      = filePath;
                            db.JDE_Files.Add(item);
                            db.SaveChanges();

                            JDE_Logs Log = new JDE_Logs {
                                UserId = UserId, Description = "Utworzenie pliku", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item)
                            };
                            db.JDE_Logs.Add(Log);
                            db.SaveChanges();
                        }
                    }

                    JDE_FileAssigns FileAssing = new JDE_FileAssigns {
                        FileId = item.FileId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId, PartId = PartId, PlaceId = PlaceId, ProcessId = ProcessId, UserLogId = UserLogId
                    };
                    string word = "";
                    if (PlaceId != null)
                    {
                        word = "zasobu";
                    }
                    else if (PartId != null)
                    {
                        word = "części";
                    }
                    else if (ProcessId != null)
                    {
                        word = "zgłoszenia";
                    }
                    else
                    {
                        word = "logu użytkownika";
                    }
                    db.JDE_FileAssigns.Add(FileAssing);
                    db.SaveChanges();
                    JDE_Logs Log2 = new JDE_Logs {
                        UserId = UserId, Description = $"Przypisanie pliku do {word}", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item)
                    };
                    db.JDE_Logs.Add(Log2);
                    db.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK, item));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }