Esempio n. 1
0
        public static void ProcessRequest(LoginUser loginUser, int organizationID, HttpContext context, int?_id, string _ratingImage, List <string> segments)
        {
            int id;

            if (int.TryParse(segments[segments.Count - 1], out id))
            {
                _id = id;
                segments.RemoveAt(segments.Count - 1);
            }

            if (segments[segments.Count - 1] == "ratingpositive" || segments[segments.Count - 1] == "ratingneutral" || segments[segments.Count - 1] == "ratingnegative")
            {
                _ratingImage = segments[segments.Count - 1];
                segments.RemoveAt(segments.Count - 1);
            }


            AttachmentPath.Folder folder = GetFolder(context, segments.ToArray());
            if (folder == AttachmentPath.Folder.None)
            {
                throw new Exception("Invalid path.");
            }

            AttachmentProxy.References refType = AttachmentPath.GetFolderReferenceType(folder);
            if (refType == AttachmentProxy.References.None)
            {
                SaveFilesOld(loginUser, organizationID, _id, _ratingImage, context, folder);
            }
            else
            {
                SaveFiles(loginUser, context, folder, organizationID, _id);
            }
        }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        _manager.AjaxSettings.Clear();

        _refID   = int.Parse(Request["RefID"]);
        _refType = (AttachmentProxy.References) int.Parse(Request["RefType"]);
    }
Esempio n. 3
0
        public static AttachmentProxy CreateAttachment(int organizationID, AttachmentProxy.References refType, int refID, string path)
        {
            ActionAttachmentProxy attachmentProxy = (ActionAttachmentProxy)AttachmentProxy.ClassFactory(AttachmentProxy.References.Actions);

            attachmentProxy.OrganizationID = organizationID;
            attachmentProxy.ActionID       = refID;
            attachmentProxy.FileName       = "stuff";
            attachmentProxy.FilePathID     = 3;
            attachmentProxy.FileSize       = 256;
            attachmentProxy.FileType       = "text something...";
            attachmentProxy.Path           = path; // IAttachmentDestination
            return(attachmentProxy);
        }
Esempio n. 4
0
        public static void CreateAttachment(string savePath, int organizationId, AttachmentProxy.References refType, LoginUser user, HttpWebResponse httpWebResponse)
        {
            Attachments attachments = new Attachments(user);
            Attachment  attachment  = attachments.AddNewAttachment();

            attachment.RefType        = refType;
            attachment.RefID          = user.UserID;
            attachment.OrganizationID = organizationId;
            attachment.FileName       = Path.GetFileName(savePath);
            attachment.Path           = savePath;
            attachment.FileType       = string.IsNullOrEmpty(httpWebResponse.ContentType) ? "application/octet-stream" : httpWebResponse.ContentType;
            attachment.FileSize       = httpWebResponse.ContentLength;
            //attachment.FilePathID = 3;
            attachments.Save();
        }
Esempio n. 5
0
        public static string GetAttachmentPath(LoginUser loginUser, AttachmentProxy.References refType, int refID, int filePathID)
        {
            string root = AttachmentPath.GetRoot(loginUser, loginUser.OrganizationID, filePathID);
            string type = null;

            switch (refType)
            {
            case AttachmentProxy.References.None:
                break;

            case AttachmentProxy.References.Actions: type = "Actions"; break;

            case AttachmentProxy.References.Organizations: type = "OrganizationAttachments"; break;

            case AttachmentProxy.References.ProductVersions:
                break;

            case AttachmentProxy.References.Users:
                break;

            case AttachmentProxy.References.Contacts:
                break;

            case AttachmentProxy.References.UserPhoto:
                type = "UserPhoto";
                break;

            default:
                break;
            }

            if (type == null)
            {
                throw new Exception("Error: Cannot convert reference type to string.");
            }

            return(Path.Combine(Path.Combine(root, type), refID.ToString()) + "\\");
        }
Esempio n. 6
0
        public static void CreateAttachment(string savePath, int organizationId, AttachmentProxy.References refType, int userID, System.Net.HttpWebResponse httpWebResponse)
        {
            try
            {
                using (ConnectionContext connection = new ConnectionContext())
                {
                    AttachmentProxy attachment = AttachmentProxy.ClassFactory(refType);
                    attachment.RefID          = userID;
                    attachment.OrganizationID = organizationId;
                    attachment.FileName       = Path.GetFileName(savePath);
                    attachment.Path           = savePath;
                    attachment.FileType       = string.IsNullOrEmpty(httpWebResponse.ContentType) ? "application/octet-stream" : httpWebResponse.ContentType;
                    attachment.FileSize       = httpWebResponse.ContentLength;

                    IAttachmentDestination model = ClassFactory(connection, attachment);
                    Data_API.Create(model as IDNode, attachment);
                }
            }
            catch (Exception ex)
            {
                // TODO - tell user we failed
                Data_API.LogMessage(ActionLogType.Insert, (ReferenceType)refType, 0, "choke", ex);
            }
        }
Esempio n. 7
0
        public static string SaveAttachment(LoginUser loginUser, string contentType, long contentLength, string directory, string fileName, AttachmentProxy.References _refType, int _refID, string description)
        {
            Attachments attachments = new Attachments(loginUser);

            Attachment attachment = attachments.AddNewAttachment();

            attachment.RefType        = _refType;
            attachment.RefID          = _refID;
            attachment.OrganizationID = loginUser.OrganizationID;
            attachment.FileName       = fileName;
            //attachment.Path = Path.Combine(directory, fileName);
            attachment.FilePathID  = 3;
            attachment.FileType    = string.IsNullOrEmpty(contentType) ? "application/octet-stream" : contentType;
            attachment.FileSize    = contentLength;
            attachment.Description = description;

            Directory.CreateDirectory(directory);
            attachments.Save();
            return(attachment.Path);
        }
        public static string CreateAttachmentDirectory(LoginUser loginUser, int parentOrganizationID, AttachmentProxy.References refType, int refID)
        {
            // hub save only saves two types of attachments on this code path
            AttachmentPath.Folder folder = AttachmentPath.Folder.CustomerHubLogo;
            if (refType == AttachmentProxy.References.Actions)
            {
                folder = AttachmentPath.Folder.Actions;
            }

            string path = AttachmentPath.GetPath(loginUser, parentOrganizationID, folder);

            path = Path.Combine(path, refID.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return(path);
        }
        public static bool DownloadImage(string imageUrl, string savePath, int organizationId, AttachmentProxy.References refType, TeamSupport.Data.LoginUser user, out string resultMessage)
        {
            resultMessage = string.Empty;
            Image imageDownloaded = null;

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(imageUrl);

                using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    using (Stream stream = httpWebResponse.GetResponseStream())
                    {
                        if (httpWebResponse.StatusCode == HttpStatusCode.OK && httpWebResponse.ContentType.Contains("image"))
                        {
                            imageDownloaded = Image.FromStream(stream);

                            if (imageDownloaded != null)
                            {
                                if (!DownloadCheckingIfDifferent(imageDownloaded, savePath, ImageFormat.Png))
                                {
                                    resultMessage   = "The image is the same as the current one. It was not downloaded.";
                                    imageDownloaded = null;
                                }
                                else
                                {
                                    resultMessage = string.Format("Image saved: {0}", savePath);

                                    //ModelAPI.AttachmentAPI.CreateAttachment(savePath, organizationId, refType, user.UserID, httpWebResponse);
                                    TeamSupport.Data.Quarantine.ServiceQ.CreateAttachment(savePath, organizationId, refType, user, httpWebResponse);
                                }
                            }
                            else
                            {
                                resultMessage = "The image was not downloaded, but no error was thrown.";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultMessage = string.Format("{0}\n{1}", ex.Message, ex.StackTrace);
            }

            return(imageDownloaded != null);
        }
Esempio n. 10
0
 public PathMap(string path, AttachmentProxy.References refType)
 {
     _path    = path;
     _refType = refType;
 }
Esempio n. 11
0
        public static string DeleteAttachment(int attachmentID, AttachmentProxy.References verifyRefType, int?verifyRefID = null)
        {
            string result = null;

            try
            {
                using (ConnectionContext connection = new ConnectionContext())
                {
                    // validate args
                    AttachmentProxy proxy = Data_API.ReadRefTypeProxy <AttachmentProxy>(connection, attachmentID);
                    result = proxy.FileName;

                    if (verifyRefType == AttachmentProxy.References.None) // support delete no matter the type?
                    {
                        verifyRefType = proxy.RefType;
                    }

                    if (!verifyRefID.HasValue) // if no explicit refID provided, use the proxy
                    {
                        verifyRefID = proxy.RefID;
                    }

                    // type safe construction
                    IAttachmentDestination model = null;
                    switch (verifyRefType)
                    {
                    case AttachmentProxy.References.Actions:
                        model = new ActionModel(connection, verifyRefID.Value);
                        if (!(model as ActionModel).CanEdit())      // is user authorized to do the delete?
                        {
                            return(null);
                        }
                        break;

                    case AttachmentProxy.References.Assets:
                        model = new AssetModel(connection, verifyRefID.Value);
                        break;

                    //case AttachmentProxy.References.ChatAttachments:
                    //    break;
                    case AttachmentProxy.References.CompanyActivity:
                    {
                        int organizationID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {verifyRefID.Value}").Min();
                        model = new NoteModel(new OrganizationModel(connection, organizationID), verifyRefID.Value);
                    }
                    break;

                    case AttachmentProxy.References.ContactActivity:
                    {
                        int userID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {verifyRefID.Value}").Min();
                        model = new NoteModel(new UserModel(connection, userID), verifyRefID.Value);
                    }
                    break;

                    //case AttachmentProxy.References.Contacts:
                    //    break;
                    //case AttachmentProxy.References.CustomerHubLogo:
                    //    break;
                    //case AttachmentProxy.References.None:
                    //    break;
                    case AttachmentProxy.References.Organizations:
                        model = new OrganizationModel(connection, verifyRefID.Value);
                        break;

                    case AttachmentProxy.References.ProductVersions:
                        model = new ProductVersionModel(connection, verifyRefID.Value);
                        break;

                    case AttachmentProxy.References.Tasks:
                        model = new TaskModel(connection, verifyRefID.Value);
                        break;

                    case AttachmentProxy.References.UserPhoto:
                    case AttachmentProxy.References.Users:
                        model = new UserModel(connection, verifyRefID.Value);
                        break;

                    case AttachmentProxy.References.WaterCooler:
                        model = new WatercoolerMsgModel(connection, verifyRefID.Value);
                        break;

                    default:
                        if (Debugger.IsAttached)
                        {
                            Debugger.Break();
                        }
                        throw new Exception($"unrecognized RefType {verifyRefType} in DeleteAttachment");
                    }

                    // do the delete
                    Data_API.Delete(new AttachmentModel(model, attachmentID));
                    AttachmentFile file = new AttachmentFile(model, proxy);
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                Data_API.LogMessage(ActionLogType.Delete, ReferenceType.Attachments, attachmentID, "Unable to delete attachment", ex);
            }
            return(result);
        }
Esempio n. 12
0
        private static IAttachmentDestination ClassFactory(ConnectionContext connection, AttachmentProxy.References refType, int refID)
        {
            switch (refType)
            {
            case AttachmentProxy.References.Actions: return(new ActionModel(connection, refID));

            case AttachmentProxy.References.Assets: return(new AssetModel(connection, refID));

            case AttachmentProxy.References.ChatAttachments: return(new ChatModel(connection, refID));

            case AttachmentProxy.References.CompanyActivity:
            {
                // Attachment => Note => Organization
                int organizationID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {refID}").Min();
                return(new NoteModel(new OrganizationModel(connection, organizationID), refID));
            }

            case AttachmentProxy.References.ContactActivity:
            {
                // Attachment => Note => User
                int userID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {refID}").Min();
                return(new NoteModel(new UserModel(connection, userID), refID));
            }

            //case AttachmentProxy.References.Contacts: return new ContactModel(connection, refID);
            //case AttachmentProxy.References.CustomerHubLogo: return new CustomerHubLogoModel(connection, refID);
            case AttachmentProxy.References.Organizations: return(new OrganizationModel(connection, refID));

            case AttachmentProxy.References.ProductVersions: return(new ProductVersionModel(connection, refID));

            case AttachmentProxy.References.Tasks: return(new TaskModel(connection.Organization, refID));

            case AttachmentProxy.References.UserPhoto:
            case AttachmentProxy.References.Users:
                return(new UserModel(connection, refID));

            case AttachmentProxy.References.WaterCooler: return(new WatercoolerMsgModel(connection, refID));

            default:
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }
                throw new Exception($"bad ReferenceType {refType}");
            }
        }
Esempio n. 13
0
        static void SaveFiles(LoginUser loginUser, HttpContext context, AttachmentPath.Folder folder, int organizationID, int?itemID)
        {
            List <TeamSupport.Handlers.UploadResult> result = new List <TeamSupport.Handlers.UploadResult>();

            AttachmentProxy.References refType = AttachmentPath.GetFolderReferenceType(folder);

            string path = AttachmentPath.GetPath(LoginUser.Anonymous, organizationID, folder, 3);

            if (itemID != null)
            {
                path = Path.Combine(path, itemID.ToString());
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            HttpFileCollection files = context.Request.Files;

            for (int i = 0; i < files.Count; i++)
            {
                if (files[i].ContentLength > 0)
                {
                    string fileName = TeamSupport.Handlers.UploadUtils.RemoveSpecialCharacters(DataUtils.VerifyUniqueUrlFileName(path, Path.GetFileName(files[i].FileName)));

                    files[i].SaveAs(Path.Combine(path, fileName));
                    if (refType != AttachmentProxy.References.None && itemID != null)
                    {
                        Attachment attachment = (new Attachments(loginUser)).AddNewAttachment();
                        attachment.RefType        = refType;
                        attachment.RefID          = (int)itemID;
                        attachment.OrganizationID = organizationID;
                        attachment.FileName       = fileName;
                        attachment.Path           = Path.Combine(path, fileName);
                        attachment.FileType       = files[i].ContentType;
                        attachment.FileSize       = files[i].ContentLength;
                        attachment.FilePathID     = 3;
                        if (context.Request.Form["description"] != null)
                        {
                            attachment.Description = context.Request.Form["description"].Replace("\n", "<br />");
                        }
                        if (context.Request.Form["productFamilyID"] != null && context.Request.Form["productFamilyID"] != "-1")
                        {
                            attachment.ProductFamilyID = Int32.Parse(context.Request.Form["productFamilyID"]);
                        }

                        attachment.Collection.Save();
                        result.Add(new TeamSupport.Handlers.UploadResult(fileName, attachment.FileType, attachment.FileSize, attachment.AttachmentID));
                    }
                    else
                    {
                        switch (refType)
                        {
                        default:
                            break;
                        }
                    }
                }
            }
            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(DataUtils.ObjectToJson(result.ToArray()));
        }