Exemple #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        string szClass = util.GetStringParam(Request, "r");
        string szKey   = util.GetStringParam(Request, "k");
        string szThumb = util.GetStringParam(Request, "t");

        MFBImageInfo.ImageClass ic = MFBImageInfo.ImageClass.Unknown;
        if (Enum.TryParse <MFBImageInfo.ImageClass>(szClass, out MFBImageInfo.ImageClass result))
        {
            ic = result;
        }

        this.Title = szThumb;
        if (ic != MFBImageInfo.ImageClass.Unknown && !String.IsNullOrEmpty(szKey) && !String.IsNullOrEmpty(szThumb) && !szThumb.Contains("?"))  // Googlebot seems to be adding "?resize=300,300
        {
            MFBImageInfo mfbii = new MFBImageInfo(ic, szKey, szThumb);
            if (mfbii == null)
            {
                throw new MyFlightbookException("mfbii null in ViewPic");
            }

            if (Request == null)
            {
                throw new MyFlightbookException("Null request in ViewPic");
            }

            string ua = String.IsNullOrEmpty(Request.UserAgent) ? string.Empty : Request.UserAgent.ToUpperInvariant();
            if (mfbii.ImageType == MFBImageInfo.ImageFileType.S3VideoMP4 && !ua.Contains("IPHONE") && !ua.Contains("IPAD"))
            {
                // For android devices, need to put it into actual HTML for it to play in-line in the browser.
                mfbEditableImage1.MFBImageInfo = mfbii;
            }
            else
            {
                try
                {
                    Response.Redirect(mfbii.ResolveFullImage());
                }
                catch (ArgumentException ex)
                {
                    throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Argument Exception in ViewPic key={0}\r\nthumb={1}\r\n", szKey, szThumb), ex);
                }
                catch (System.Web.HttpUnhandledException ex)
                {
                    throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Argument Exception in ViewPic key={0}\r\nthumb={1}\r\n", szKey, szThumb), ex);
                }
                catch (NullReferenceException ex)
                {
                    throw new MyFlightbookException("Null reference on resolve full image (but where?) ", ex);
                }
            }
        }
        else
        {
            Response.Clear();
            Response.StatusCode = 404;
            Response.End();
        }
    }
Exemple #2
0
 public PendingVideo(string guid, string jobID, string comment, MFBImageInfo.ImageClass ic, string key, string bucket)
 {
     GUID           = guid;
     JobID          = jobID;
     Comment        = comment;
     Class          = ic;
     Key            = key;
     Bucket         = bucket;
     SubmissionTime = DateTime.Now;
 }
    protected void Page_Load(object sender, EventArgs e)
    {
        string szClass = util.GetStringParam(Request, "r");
        string szKey   = util.GetStringParam(Request, "k");
        string szThumb = util.GetStringParam(Request, "t");

        MFBImageInfo.ImageClass ic = MFBImageInfo.ImageClass.Unknown;
        if (Enum.TryParse <MFBImageInfo.ImageClass>(szClass, out MFBImageInfo.ImageClass result))
        {
            ic = result;
        }

        this.Title = szThumb;
        if (ic != MFBImageInfo.ImageClass.Unknown && !String.IsNullOrEmpty(szKey) && !String.IsNullOrEmpty(szThumb) && !szThumb.Contains("?"))  // Googlebot seems to be adding "?resize=300,300
        {
            MFBImageInfo mfbii = new MFBImageInfo(ic, szKey, szThumb);
            if (mfbii == null)
            {
                throw new MyFlightbookException("mfbii null in ViewPic");
            }

            if (Request == null)
            {
                throw new MyFlightbookException("Null request in ViewPic");
            }

            try
            {
                Response.Redirect(mfbii.ResolveFullImage());
            }
            catch (Exception ex) when(ex is ArgumentException || ex is System.Web.HttpUnhandledException)
            {
                throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Argument Exception in ViewPic key={0}\r\nthumb={1}\r\n", szKey, szThumb), ex);
            }
            catch (NullReferenceException ex)
            {
                throw new MyFlightbookException("Null reference on resolve full image (but where?) ", ex);
            }
        }
        else
        {
            Response.Clear();
            Response.StatusCode = 404;
            Response.End();
        }
    }
        /// <summary>
        /// ADMIN ONLY - Remove images from S3 LIVE BUCKET that are orphaned (no reference from live site)
        /// </summary>
        /// <param name="ic"></param>
        /// <param name="handleS3Object"></param>
        public static void ADMINDeleteS3Orphans(MFBImageInfo.ImageClass ic, Action <long, long, long, long> onSummary, Action onS3EnumDone, Func <string, int, bool> onDelete)
        {
            string szBasePath = MFBImageInfo.BasePathFromClass(ic);

            if (szBasePath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase))
            {
                szBasePath = szBasePath.Substring(1);
            }

            Regex r = new Regex(String.Format(CultureInfo.InvariantCulture, "{0}(.*)/(.*)(\\.jpg|\\.jpeg|\\.pdf|\\.s3pdf|\\.mp4)", szBasePath), RegexOptions.IgnoreCase);

            long cBytesToFree  = 0;
            long cBytesOnS3    = 0;
            long cFilesOnS3    = 0;
            long cOrphansFound = 0;

            using (IAmazonS3 s3 = AWSConfiguration.S3Client())
            {
                ListObjectsRequest request = new ListObjectsRequest()
                {
                    BucketName = AWSConfiguration.S3BucketName, Prefix = szBasePath
                };

                /*
                 * Need to get the files from S3 FIRST, otherwise there is a race condition
                 * I.e., if we get files from the DB, then get files from S3, a file could be added to the db AFTER our query
                 * but BEFORE we retrieve it from the S3 listing, and we will thus treat it as an orphan and delete it.
                 * But since the file is put into the DB before being moved to S3, if we get all the S3 files and THEN
                 * get the DB references, we will always have a subset of the valid S3 files, which prevents a false positive
                 * orphan identification.
                 */

                List <S3Object> lstS3Objects = new List <S3Object>();
                // Get the list of S3 objects
                do
                {
                    ListObjectsResponse response = s3.ListObjects(request);

                    cFilesOnS3 += response.S3Objects.Count;
                    foreach (S3Object o in response.S3Objects)
                    {
                        cBytesOnS3 += o.Size;
                        lstS3Objects.Add(o);
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        request.Marker = response.NextMarker;
                    }
                    else
                    {
                        request = null;
                    }
                } while (request != null);

                onS3EnumDone?.Invoke();

                // Now get all of the images in the class and do orphan detection
                Dictionary <string, MFBImageInfo> dictDBResults = MFBImageInfo.AllImagesForClass(ic);

                long  cOrphansLikely = Math.Max(lstS3Objects.Count - dictDBResults.Keys.Count, 1);
                Regex rGuid          = new Regex(String.Format(CultureInfo.InvariantCulture, "^({0})?([^_]*).*", MFBImageInfo.ThumbnailPrefixVideo), RegexOptions.Compiled); // for use below in extracting GUIDs from video thumbnail and video file names.
                lstS3Objects.ForEach((o) =>
                {
                    Match m = r.Match(o.Key);
                    if (m.Groups.Count < 3)
                    {
                        return;
                    }

                    string szKey  = m.Groups[1].Value;
                    string szName = m.Groups[2].Value;
                    string szExt  = m.Groups[3].Value;

                    bool fPDF      = (String.Compare(szExt, FileExtensions.PDF, StringComparison.InvariantCultureIgnoreCase) == 0);
                    bool fVid      = (String.Compare(szExt, FileExtensions.MP4, StringComparison.InvariantCultureIgnoreCase) == 0);
                    bool fVidThumb = Regex.IsMatch(szExt, FileExtensions.RegExpImageFileExtensions) && szName.StartsWith(MFBImageInfo.ThumbnailPrefixVideo, StringComparison.InvariantCultureIgnoreCase);
                    bool fJpg      = (String.Compare(szExt, FileExtensions.JPG, StringComparison.InvariantCultureIgnoreCase) == 0 || String.Compare(szExt, FileExtensions.JPEG, StringComparison.InvariantCultureIgnoreCase) == 0);

                    string szThumb = string.Empty;
                    if (fPDF)
                    {
                        szThumb = szName + FileExtensions.S3PDF;
                    }
                    else if (fVid || fVidThumb)
                    {
                        // This is a bit of a hack, but we have two files on the server for a video, neither of which precisely matches what's in the database.
                        // The video file is {guid}.mp4 or {guid}_.mp4, the thumbnail on S3 is v_{guid}_0001.jpg.
                        // So we grab the GUID and see if we have a database entry matching that guid.
                        Match mGuid       = rGuid.Match(szName);
                        szThumb           = (mGuid.Groups.Count >= 3) ? mGuid.Groups[2].Value : szName + szExt;
                        string szMatchKey = dictDBResults.Keys.FirstOrDefault(skey => skey.Contains(szThumb));
                        if (!String.IsNullOrEmpty(szMatchKey))  // leave it in the dictionary - don't remove it - because we may yet hit the Mp4 or the JPG.
                        {
                            return;
                        }
                    }
                    else if (fJpg)
                    {
                        szThumb = MFBImageInfo.ThumbnailPrefix + szName + szExt;
                    }

                    string szPrimary = MFBImageInfo.PrimaryKeyForValues(ic, szKey, szThumb);

                    // if it is found, super - remove it from the dictionary (for performance) and return
                    if (dictDBResults.ContainsKey(szPrimary))
                    {
                        dictDBResults.Remove(szPrimary);
                    }
                    else
                    {
                        cOrphansFound++;
                        cBytesToFree += o.Size;
                        if (onDelete(o.Key, (int)((100 * cOrphansFound) / cOrphansLikely)))
                        {
                            // Make sure that the item
                            DeleteObjectRequest dor = new DeleteObjectRequest()
                            {
                                BucketName = AWSConfiguration.S3BucketName, Key = o.Key
                            };
                            s3.DeleteObject(dor);
                        }
                    }
                });

                onSummary?.Invoke(cFilesOnS3, cBytesOnS3, cOrphansFound, cBytesToFree);
            }
        }
        protected void DeleteS3Orphans(MFBImageInfo.ImageClass ic)
        {
            LiteralControl lc = new LiteralControl(String.Format(CultureInfo.InvariantCulture, "<iframe src=\"{0}\" width=\"600\" height=\"300\"></iframe>", String.Format(CultureInfo.InvariantCulture, "{0}?dels3orphan=1&r={1}{2}", ResolveClientUrl("AdminImages.aspx"), ic.ToString(), ckPreviewOnly.Checked ? "&preview=1" : string.Empty)));

            plcDBSync.Controls.Add(lc);
        }
Exemple #6
0
 public ImageList(MFBImageInfo.ImageClass imageClass, string szKey, MFBImageInfo[] rgImages = null)
 {
     Class   = imageClass;
     m_szKey = szKey;
     rgMfbii = rgImages;
 }
Exemple #7
0
 /// <summary>
 /// Persists the pending image, returning the resulting MFBImageInfo object.
 /// </summary>
 /// <returns>The newly created MFBImageInfo object</returns>
 public MFBImageInfo Commit(MFBImageInfo.ImageClass ic, string szKey)
 {
     return(new MFBImageInfo(ic, szKey, PostedFile, Comment, Location));
 }