From e367d4240f3158f7e67ba4d312e9c63356b79b9e Mon Sep 17 00:00:00 2001
From: Tyler Schicke <tyler_schicke@brown.edu>
Date: Mon, 18 Mar 2019 16:53:00 -0400
Subject: Made drop code work better

---
 .../views/collections/CollectionViewBase.tsx       | 91 +++++++++++++---------
 1 file changed, 53 insertions(+), 38 deletions(-)

(limited to 'src/client/views/collections/CollectionViewBase.tsx')

diff --git a/src/client/views/collections/CollectionViewBase.tsx b/src/client/views/collections/CollectionViewBase.tsx
index 7175e2846..f33007196 100644
--- a/src/client/views/collections/CollectionViewBase.tsx
+++ b/src/client/views/collections/CollectionViewBase.tsx
@@ -15,6 +15,8 @@ import { TupleField } from "../../../fields/TupleField";
 import { CurrentUserUtils } from "../../../server/authentication/models/current_user_utils";
 import { NumberField } from "../../../fields/NumberField";
 import { DocumentManager } from "../../util/DocumentManager";
+import request = require("request");
+import { ServerUtils } from "../../../server/ServerUtil";
 
 export interface CollectionViewProps {
     fieldKey: Key;
@@ -94,6 +96,27 @@ export class CollectionViewBase extends React.Component<SubCollectionViewProps>
         }
     }
 
+    protected getDocumentFromType(type: string, path: string, options: DocumentOptions): Opt<Document> {
+        let ctor: ((path: string, options: DocumentOptions) => Document) | undefined;
+        if (type.indexOf("image") !== -1) {
+            ctor = Documents.ImageDocument;
+        }
+        if (type.indexOf("video") !== -1) {
+            ctor = Documents.VideoDocument;
+        }
+        if (type.indexOf("audio") !== -1) {
+            ctor = Documents.AudioDocument;
+        }
+        if (type.indexOf("pdf") !== -1) {
+            ctor = Documents.PdfDocument;
+        }
+        if (type.indexOf("html") !== -1) {
+            ctor = Documents.WebDocument;
+            options = { height: options.width, ...options, };
+        }
+        return ctor ? ctor(path, options) : undefined;
+    }
+
     @action
     protected onDrop(e: React.DragEvent, options: DocumentOptions): void {
         let that = this;
@@ -115,16 +138,25 @@ export class CollectionViewBase extends React.Component<SubCollectionViewProps>
             return;
         }
 
-        console.log(e.dataTransfer.items.length);
-
         for (let i = 0; i < e.dataTransfer.items.length; i++) {
             const upload = window.location.origin + RouteStore.upload;
             let item = e.dataTransfer.items[i];
             if (item.kind === "string" && item.type.indexOf("uri") != -1) {
-                e.dataTransfer.items[i].getAsString(action((s: string) => this.props.addDocument(Documents.WebDocument(s, options), false)))
+                e.dataTransfer.items[i].getAsString(action((s: string) => {
+                    let document: Document;
+                    request.head(ServerUtils.prepend(RouteStore.corsProxy + "/" + s), (err, res, body) => {
+                        let type = res.headers["content-type"];
+                        if (type) {
+                            let doc = this.getDocumentFromType(type, s, { ...options, width: 300, nativeWidth: 300 })
+                            if (doc) {
+                                this.props.addDocument(doc, false);
+                            }
+                        }
+                    });
+                    // this.props.addDocument(Documents.WebDocument(s, { ...options, width: 300, height: 300 }), false)
+                }))
             }
             let type = item.type
-            console.log(type)
             if (item.kind == "file") {
                 let file = item.getAsFile();
                 let formData = new FormData()
@@ -136,44 +168,27 @@ export class CollectionViewBase extends React.Component<SubCollectionViewProps>
                 fetch(upload, {
                     method: 'POST',
                     body: formData
-                })
-                    .then((res: Response) => {
-                        return res.json()
-                    }).then(json => {
-
-                        json.map((file: any) => {
-                            let path = window.location.origin + file
-                            runInAction(() => {
-                                var doc: any;
-
-                                if (type.indexOf("image") !== -1) {
-                                    doc = Documents.ImageDocument(path, { ...options, nativeWidth: 200, width: 200, })
-                                }
-                                if (type.indexOf("video") !== -1) {
-                                    doc = Documents.VideoDocument(path, { ...options, nativeWidth: 300, width: 300, })
+                }).then((res: Response) => {
+                    return res.json()
+                }).then(json => {
+                    json.map((file: any) => {
+                        let path = window.location.origin + file
+                        runInAction(() => {
+                            let doc = this.getDocumentFromType(type, path, { ...options, nativeWidth: 300, width: 300 })
+
+                            let docs = that.props.Document.GetT(KeyStore.Data, ListField);
+                            if (docs != FieldWaiting) {
+                                if (!docs) {
+                                    docs = new ListField<Document>();
+                                    that.props.Document.Set(KeyStore.Data, docs)
                                 }
-                                if (type.indexOf("audio") !== -1) {
-                                    doc = Documents.AudioDocument(path, { ...options, nativeWidth: 300, width: 300, })
+                                if (doc) {
+                                    docs.Data.push(doc);
                                 }
-                                if (type.indexOf("pdf") !== -1) {
-                                    doc = Documents.PdfDocument(path, { ...options, nativeWidth: 300, width: 300, })
-                                }
-                                let docs = that.props.Document.GetT(KeyStore.Data, ListField);
-                                if (docs != FieldWaiting) {
-                                    if (!docs) {
-                                        docs = new ListField<Document>();
-                                        that.props.Document.Set(KeyStore.Data, docs)
-                                    }
-                                    if (doc) {
-                                        docs.Data.push(doc);
-                                    }
-
-                                }
-                            })
+                            }
                         })
                     })
-
-
+                })
             }
         }
     }
-- 
cgit v1.2.3-70-g09d2

