protected void LogThis(int RID, short logType, String Message)
 {
     AP_Staff_Rmb_Log entry = new AP_Staff_Rmb_Log();
     entry.Timestamp=DateTime.Now;
     entry.LogType=logType;
     entry.RID=RID;
     entry.Username = "******";
     entry.Message = Message;
     StaffRmbDataContext d = new StaffRmbDataContext();
     d.AP_Staff_Rmb_Logs.InsertOnSubmit(entry);
     d.SubmitChanges();
 }
        public void UploadEvent(object sender, MobileEventArgs args)
        {
            DateTime tokenTime = getTimeFromToken(args.token);
            int rmbNo = getRmbNoFromToken(args.token);
            if (tokenTime.AddMinutes(EXPIRE_MINUTES) <= DateTime.Now)
            {
                _view.Expire();
                int RID = _rmbs.Where(a => a.RMBNo==rmbNo).SingleOrDefault().RID;
                LogThis(RID, 0, "Attempt to upload receipt with expired token: "+args.token + " to rmb #"+getRmbNoFromToken(args.token)+", line #"+getLineNoFromToken(args.token)+", time: "+getTimeFromToken(args.token).ToShortTimeString());
                return;
            }
            int lineNo = getLineNoFromToken(args.token);

            AP_Staff_Rmb rmb = null;
            AP_Staff_RmbLine line = null;
            try {
                rmb = _rmbs.Where(a => a.SpareField4==args.token).Single();
                if (rmb.RMBNo != rmbNo) throw new Exception();
                line = _lines.Where(a => a.RmbLineNo == lineNo && a.RmbNo == rmbNo).Single();
            }
            catch { }
            try // Upload
            {
                // initialize folder/permissions
                int recnum;
                try { recnum = _images.Where(a => a.RmbLineNo == lineNo && a.RMBNo == rmbNo).Select(a => a.RecNum).Max() + 1; }
                catch
                {
                    // The above will fail for new lines.  This statement should catch those
                    try { recnum = _images.Where(a => a.RmbLineNo == null && a.RMBNo == rmbNo).Select(a => a.RecNum).Max() + 1; }
                    catch { recnum = 1; }
                }
                IFileInfo file;
                string strUrl = "";
                if (_testing)
                {
                    file = new DotNetNuke.Services.FileSystem.FileInfo() { FileId = -1 };
                }
                else
                {
                    PortalSettings PS = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
                    Filesystem.ensureFolderExists(PS.PortalId);
                    IFolderInfo imageFolder = Filesystem.getImageFolder(rmb.UserId, PS.PortalId);
                    Filesystem.checkFolderPermissions(PS.PortalId, imageFolder, rmb.UserId, null);  //no approvers list sent because it is an async function
                    string filename = "R" + rmbNo.ToString()+"L"+(lineNo<0?"New":lineNo.ToString()) + "Rec" + recnum.ToString() + ".png";
                    // save file to DNN database
                    string base64Data = Regex.Match(_view.ImageData, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
                    byte[] image_data = Convert.FromBase64String(base64Data);
                    //byte[] image_data = _view.ImageFile;
                    //_view.ImageUrl= "data:image/png;base64," + Convert.ToBase64String(image_data);
                    if (image_data == null || image_data.Length == 0) return;
                    MemoryStream image_stream = resizeImage(image_data);
                    file = FileManager.Instance.AddFile(imageFolder, filename, image_stream, false); //true is for overwrite
                    string URL = FileManager.Instance.GetUrl(file);
                    string strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
                    strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, URL);
                }
                // link file to image
                AP_Staff_RmbLine_File image = new AP_Staff_RmbLine_File() { RMBNo = rmbNo, RecNum = recnum, FileId = file.FileId };
                if (lineNo >= 0) image.RmbLineNo = lineNo; //A null RmbLineNo indicates that the line hasn't been saved yet
                image.URL = strUrl;
                StaffRmbDataContext d = new StaffRmbDataContext();
                d.AP_Staff_RmbLine_Files.InsertOnSubmit(image);
                d.SubmitChanges();
                LogThis(rmb.RID, 2, "Receipt image uploaded via mobile page");
                _view.Message = "Image uploaded.";
            }
            catch (Exception ex)
            {
                LogThis(rmb.RID, 4, "Error saving receipt image via mobile page " + ex.Message + ex.StackTrace);
                _view.Message = "Image Upload Failed";
            }
        }