Exemple #1
0
 def _get_mime_type(local_file):
     mime = MimeTypes()
     url = urllib.pathname2url(local_file)
     mime_type = mime.guess_type(url)
     return mime_type[0]
Exemple #2
0
 def get_content_type(self) -> str:
     if self.content_type:
         return self.content_type
     return MimeTypes().guess_type(
         self.target if self.target else self.source_path)[0]
Exemple #3
0
# Use the random module to choose from a list at random.
from random import choice

# Convert JSON to a Python dictionary for ease of traversal.
from json import loads

#
# Lambda functions
#

# Return a random string of a specified length.
random_str = lambda length: ''.join([choice('0123456789ABCDEF') for i in range(length)])

# Get the mimetype string from an input filename.
mimetype = lambda name: MimeTypes().guess_type(name)[0] \
    if MimeTypes().guess_type(name)[0] is not None \
    else 'application/octet-stream'

# Return a Discord snowflake from a timestamp.
snowflake = lambda timestamp_s: (timestamp_s * 1000 - 1420070400000) << 22

# Return a timestamp from a Discord snowflake.
timestamp = lambda snowflake_t: ((snowflake_t >> 22) + 1420070400000) / 1000.0


#
# Global functions
#

Exemple #4
0
def getFileEditInfo(path,
                    key,
                    alias="",
                    status="Unknown",
                    source="Unknown",
                    lock=False,
                    operation="Unknown",
                    caption="",
                    descriptions=""):
    fil_uuid = str(uuid.uuid4())
    con_id = ""
    mat_id = ""
    relative_path = ""
    operating_application = "Custom Python Code"

    if lock == True:
        is_lock = "TRUE"
    else:
        is_lock = "FALSE"

    mime = MimeTypes()
    mime_type = mime.guess_type(path)

    # Extract the directories from the full path.
    dirs = path.split("/")

    # Get the indecies of starting and ending point for the relative path.
    start = dirs.index(key)
    end = len(dirs)

    # Reconstruct the relative path to the source.
    for i in range(start, end):
        # Get the consolidation uuid.
        if str(dirs[i - 1]) == "Consolidation":
            if len(dirs[i]) == 36:
                con_id = dirs[i]
            else:
                print("File Name Error in Consolidation")

        # Get the materials uuid.
        if str(dirs[i - 1]) == "Materials":
            if len(dirs[i]) == 36:
                mat_id = dirs[i]
            else:
                print("File Name Error in Materials")

        # Get the relative path
        relative_path = str(relative_path) + "/" + str(dirs[i])

    dct_file = {
        'uuid': str(uuid.uuid4()),
        'con_id': con_id,
        'mat_id': mat_id,
        'filename': relative_path.lstrip("/"),
        'mime_type': str(mime_type[0]),
        'alias': alias,
        'status': status,
        'public': "FALSE",
        'lock': is_lock,
        'source': source,
        'operation': operation,
        'operating_application': operating_application,
        'caption': caption,
        'descriptions': descriptions
    }
    return (dct_file)
Exemple #5
0
from django.shortcuts import render, get_object_or_404, get_list_or_404
from .forms import AddAssetForm
from django.views.generic import View
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
from django.db.models import Q
from mimetypes import MimeTypes
from .models import Asset
import datetime
from django.utils import timezone
import os
import mimetypes

# Create your views here.
mime = MimeTypes()


class Index(View):
    def get(self, request):
        changed_time = Asset.changed_time
        query = Q(active=True,
                  end_date__gte=datetime.datetime.now(),
                  start_date__lte=datetime.datetime.now())
        inactive_assets = Asset.objects.exclude(query).order_by('id')
        active_assets = Asset.objects.filter(query).order_by('changed_time')
        asset_form = AddAssetForm()
        #           #edit_asset_form = EditAssetForm()
        return render(request,
                      'personal/home.html',
                      context={
                          'asset_form': asset_form,
                          'active_assets': active_assets,
 def _check_file_mime_type(path):
     mime = MimeTypes()
     return mime.guess_type(path)
from mimetypes import MimeTypes
from base64 import b64encode

mt = MimeTypes()


def lambda_handler(event, context):
    path: str = event["path"]
    print(f"Path is {path}")
    if path.startswith("/showMeTheCurrency"):
        path = path[18:]

    if path == "":
        filename = "index.html"
    else:
        path = path.lstrip("/")
        filename = path
    print(f"Filename is {filename}")
    try:
        with open(filename, "rb") as f:
            content = f.read()

        if filename.endswith(".png"):
            content = b64encode(content)
        content = content.decode("utf8")
        return {
            "statusCode": "200",
            "headers": {
                "Content-Type": mt.guess_type(filename)[0]
            },
            "body": content
Exemple #8
0
from importlib.machinery import SourceFileLoader, ModuleSpec
from pathlib import Path
import sys, textwrap, warnings, mistune
PY2 = sys.version_info.major is 2
from collections import UserList
from importlib.util import spec_from_loader
from IPython.core.inputtransformer import InputTransformer
from fnmatch import fnmatch
from functools import partial, partialmethod
from IPython import get_ipython, display
from IPython.core.interactiveshell import InteractiveShell
from nbconvert import filters
from nbformat import reads, v4, NotebookNode
from toolz.curried import identity as identity_, partial
from mimetypes import MimeTypes
mimetypes = MimeTypes()
from nbconvert.exporters.templateexporter import TemplateExporter
import argparse

exporter = TemplateExporter()
HR = '---'


def identity(*args, **kwargs):
    return args[0]


def macro(code: str) -> Tuple[display.DisplayObject]:
    """
    >>> url = "https://test.com"
    >>> assert macro(url) and macro(''' {}
Exemple #9
0
import time
import random
import os
import base64
import binascii

from mimetypes import MimeTypes
from indico.web.wsgi import webinterface_handler_config as apache
from indico.web.wsgi.webinterface_handler_config import \
    SERVER_RETURN, HTTP_NOT_FOUND

# Legacy imports

from MaKaC.common import Config

_mimes = MimeTypes(strict=False)
_mimes.suffix_map.update({'.tbz2': '.tar.bz2'})
_mimes.encodings_map.update({'.bz2': 'bzip2'})


def stream_file(req,
                fullpath,
                fullname=None,
                mime=None,
                encoding=None,
                etag=None,
                md5=None,
                location=None):
    """This is a generic function to stream a file to the user.
    If fullname, mime, encoding, and location are not provided they will be
    guessed based on req and fullpath.
 def get_content_type_header(file_path: str) -> str:
     mime = MimeTypes()
     return f"Content-Type: {mime.guess_type(file_path)[0]}"
Exemple #11
0
"""
This module stores shortcuts for commonly performed actions
can be used externally
"""

from mimetypes import MimeTypes
from urllib.request import pathname2url
import discord
from emoji import UNICODE_EMOJI as uemoji
from _io import TextIOWrapper

MIME = MimeTypes()


def try_file(name, content='[]') -> TextIOWrapper:
    """Check if a file exists and create it with default content if it doesnt"""
    try:
        return open(name)
    except FileNotFoundError:
        open(name, 'w').write(content)
        print(f'Generated {name} file')
        return open(name)


def quick_embed(ctx,
                title: str,
                description: str = None,
                colour: int = 0x023cfc) -> discord.Embed:
    """Creates an embed with a colour, title and description based on context"""
    try:
        colour = ctx.me.colour
Exemple #12
0
def fix_v3_value(value, v4nodeinfo):
    """in some cases, the v3 data must be modified before it can be saved
    into a v4 tile. this conversion is based on the v4 node datatype."""

    datatype = v4nodeinfo['v4_datatype']
    # convert the WKT geometry representation from v3 to a geojson feature
    # collection which is what is needed in v4
    if datatype == "geojson-feature-collection":

        # assume that if a dictionary is passed in, it is already geojson
        if not isinstance(value, dict):
            geom = json.loads(GEOSGeometry(value).geojson)
            value = {
                "type":
                "FeatureCollection",
                "features": [{
                    "type": "Feature",
                    "id": str(uuid.uuid4()),
                    "geometry": geom,
                    "properties": {}
                }]
            }

    # sanitize number input
    elif datatype == "number":
        value = value.replace(",", "")
        try:
            value = int(value)
        except ValueError:
            value = float(value)

    elif datatype == "date":
        # kinda hacky but need to remove the T00:00:00 from the end of the
        # date string, and datetime doesn't handle dates before 1900 so just
        # use basic string slicing, as all v3 dates will end with T00:00:00.
        value = value[:-9]

    elif datatype == "file-list":

        filename = os.path.basename(value)
        fullpath = os.path.join(settings.MEDIA_ROOT, 'uploadedfiles', filename)

        # handle the fact that file names could be too long, > 100 characters.
        # a bit more complicated that you would expect, in order to accommodate
        # iterative development.
        shortenedpath = ""
        if len(filename) > 100:
            shortname = os.path.splitext(filename)[0][:96] + os.path.splitext(
                filename)[1]
            shortenedpath = fullpath.replace(filename, shortname)

        if not os.path.isfile(fullpath) and not os.path.isfile(shortenedpath):
            print "expected file doesn't exist: {}".format(fullpath)
            print "all files must be transferred before migration can continue"
            exit()

        if shortenedpath != "":
            if not os.path.isfile(shortenedpath):
                os.rename(fullpath, shortenedpath)
            fullpath = shortenedpath
            filename = shortname

        # create ORM file object
        file_obj = File()
        file_obj.path = filename

        try:
            file_obj.save()
        except Exception as e:
            print "Error saving file: {}".format(fullpath)
            print e
            exit()

        # construct the full json needed to define a file-list node in a tile
        stats = os.stat(fullpath)
        type = MimeTypes().guess_type(fullpath)[0]
        value = [{
            "name":
            filename,
            "file_id":
            str(file_obj.pk),
            "url":
            str(file_obj.path.url).replace("files/", "files/uploadedfiles/"),
            "status":
            'uploaded',
            "index":
            0,
            "lastModified":
            stats.st_mtime,
            "height":
            None,
            "width":
            None,
            "type":
            type,
            "size":
            stats.st_size,
            # "content":"blob:http://localhost:8000/24dd8daa-da29-49ec-805a-3dd8a683162c",
            "accepted":
            True
        }]

    return value
Exemple #13
0
def editFileContent():
    try:
        #Initialize
        result = {'file' : '',
                   'mime' : '',
                   'message' : '',
                   'message_css' : '',
                   'download' : '',
                   'showDownload' : False,
                   'containerScroll' : True,
                   'content' : '',
                   'keywords' : ''}
    
        #json_data = request.json
        
        json_data = request.get_json(force=True)
        
        #file = json_data['path']
        file = os.path.join(CURRENT_DIR, json_data['path'].replace("../",""))
        fileContent = json_data['content'].replace("<br>", "\n")
        
        # cleanr = re.compile('<.*?>')
        # cleantext = re.sub(cleanr, '', fileContent)
        #print "584", cleantext
        
        # fileContent = cleantext
        
        # Providing Read, Write and Execute permission to selected featureFiles 
        exePermissionStr = "sudo " + "find " + file + " -type " + "f " + "-exec " + "chmod " + "ugo+rwx " + "{} " +"\;"
        #print "569", exePermissionStr
        ffPermissionChange = subprocess.Popen(exePermissionStr, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
        output, err = ffPermissionChange.communicate()
        temp = ffPermissionChange.returncode
        #print "572", temp

        if temp == 0:
            with open(file, "w") as f:
                #print "575"
                f.write(fileContent)
        else: 
            print "Write permission not granted to the feature file"

        mime = MimeTypes()
        mime_type = mime.guess_type(urllib.pathname2url(file))[0] if mime.guess_type(urllib.pathname2url(file))[0] else 'text/plain' 
        result['file'] = file
        result['mime'] = mime_type
        download_html = ''
        download_html+='<div class="">'
        download_html+='<a class="btn-download btn btn-lg btn-primary" href="' + file + '" target="_blank"><i class="glyphicon glyphicon-download-alt"></i> Download</a>'
        download_html+='</div>'
        result['download'] = download_html
        
        supported_application = ['application/xml', 'text/x-php', 'text/plain', 'text/xml', 'text/csv' ]
        mime = mime_type.split('/')
        file_mime = mime[0]
        if mime_type in supported_application:
            content = ''
            words = ''
            
            file_ext = file.split('.')
            
            if file_ext[len(file_ext)-1] == "properties":
                with open(file) as f:
                    for line in f:
                        content += line
            elif file_ext[len(file_ext)-1] == "feature":
                #print "569"
                content = open(file).read()
                x = open(file, "r")
                words = [w for w in x.read().split() if w[0] == "@"]   
                
            else:
                content = open(file).read()

            result['content'] = content
            
            result['containerScroll'] = True
            result['showDownload'] = False
            result['keywords'] = words
            
        elif (file_mime == 'text'):
            html_content = ''
            html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% ">'
            html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">'
            html_content+='</object>'
            result['content'] = html_content
            result['containerScroll'] = False
            result['showDownload'] = False
        else:
            html_content = ''
            html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% " class="hidden">'
            html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">'
            html_content+='</object>'
            result['content'] = html_content
            result['containerScroll'] = False
            result['showDownload'] = True
       
        return jsonify(status='OK',result=result)
        
    except Exception,e:
        return jsonify(status='ERROR',message=str(e))
Exemple #14
0
def getFileContent():
    try:
        #Initialize
        result = {'file' : '',
                   'mime' : '',
                   'message' : '',
                   'message_css' : '',
                   'download' : '',
                   'showDownload' : False,
                   'containerScroll' : True,
                   'content' : '',
                   'keywords' : ''}
    
        #json_data = request.json
        
        json_data = request.get_json(force=True)
        
        #file = json_data['path']
        file = os.path.join(CURRENT_DIR, json_data['path'].replace("../",""))
        
        mime = MimeTypes()
        mime_type = mime.guess_type(urllib.pathname2url(file))[0] if mime.guess_type(urllib.pathname2url(file))[0] else 'text/plain' 
        result['file'] = file
        result['mime'] = mime_type
        download_html = ''
        download_html+='<div class="">'
        download_html+='<a class="btn-download btn btn-lg btn-primary" href="' + file + '" target="_blank"><i class="glyphicon glyphicon-download-alt"></i> Download</a>'
        download_html+='</div>'
        result['download'] = download_html
        
        supported_application = ['application/xml', 'text/x-php', 'text/plain', 'text/xml', 'text/csv']
        mime = mime_type.split('/')
        file_mime = mime[0]
        if mime_type in supported_application:
            content = ''
            words = ''
            
            file_ext = file.split('.')
            
            if file_ext[len(file_ext)-1] == "properties":
                with open(file) as f:
                    for line in f:
                        content += line
            elif file_ext[len(file_ext)-1] == "feature":
                content = open(file).read()
                x = open(file, "r")
                words = [w for w in x.read().split() if w[0] == "@"]
            elif file_ext[len(file_ext)-1] == "csv":
                with open(file, 'rb') as csvfile:
                    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
                    for row in spamreader:
                        line = ' '.join(row)
                        content += line + "\n"
            else:
                content = open(file).read()
                content = unicode(content, errors='ignore')

            result['content'] = content
            
            result['containerScroll'] = True
            result['showDownload'] = False
            result['keywords'] = words
            
        elif (file_mime == 'text'):
            html_content = ''
            html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% ">'
            html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">'
            html_content+='</object>'
            result['content'] = html_content
            result['containerScroll'] = False
            result['showDownload'] = False
        else:
            html_content = ''
            html_content+= '<object data="' + file + '" type="' + mime_type + '" id="fileObject" style="width:100% " class="hidden">'
            html_content+='<embed type="' + mime_type + '" src="' + file + '" style="width:100%">'
            html_content+='</object>'
            result['content'] = html_content
            result['containerScroll'] = False
            result['showDownload'] = True
       
        return jsonify(status='OK',result=result)
    except Exception,e:
        return jsonify(status='ERROR',message=str(e))
Exemple #15
0
def processPhoto(image_file, theMIME, photo_type, identifier, fileCaption):
    image_file_thumb = image_file
    # Process the file differently depending on the MIME type
    if theMIME == "image/svg+xml":
        suffix = "svg"
        thumbSuffix = 'svg'
        thumbMIME = "image/svg+xml"

        # Read the image files into variables
        image_file.seek(0)
        dataBlob_reg = image_file.read()
        image_file.seek(0)
        dataBlob_thumbnail = image_file.read()
    elif theMIME == "image/x-ms-bmp":
        suffix = "bmp"
        thumbSuffix = 'bmp'
        thumbMIME = "image/x-ms-bmp"

        # Read the image files into variables
        image_file.seek(0)
        dataBlob_reg = image_file.read()
        image_file.seek(0)
        dataBlob_thumbnail = image_file.read()
    elif theMIME == "image/gif":
        suffix = "gif"
        thumbSuffix = 'png'
        thumbMIME = "image/png"

        # Read the image files into variables
        image_file.seek(0)
        dataBlob_reg = image_file.read()
        image_file.seek(0)
        img = Image.open(image_file)

        # Get a frame from the GIF as a PNG
        dataBlob_thumbnail = getGIFImageFramePNGBlob(img)

        # Process the thumbnail
        try:
            # Write the thumbnail into a buffer
            tempBuff = StringIO.StringIO()
            tempBuff.write(dataBlob_thumbnail.getvalue())
            tempBuff.seek(0)

            # Process the thumbnail
            basewidth_thumb = CROPPED_THUMB_WIDTH
            img_thumb = Image.open(tempBuff)
            img_thumb = removePNGAlphaLayer(img_thumb)
            wpercent_thumb = (basewidth_thumb / float(img_thumb.size[0]))
            hsize_thumb = int((float(img_thumb.size[1]) * float(wpercent_thumb)))
            img_thumb = img_thumb.resize((basewidth_thumb, hsize_thumb), Image.ANTIALIAS)
            thumbnailString_thumb = StringIO.StringIO()
            img_thumb.save(thumbnailString_thumb, format="PNG", quality=90, optimize=True)

            dataBlob_thumbnail = thumbnailString_thumb.getvalue()
        except:
            # If the thumbnail fails, put the GIF itself as the thumbnail
            thumbSuffix = 'gif'
            thumbMIME = "image/gif"
            image_file.seek(0)
            dataBlob_thumbnail = image_file.read()

    elif theMIME == "image/jpeg":
        suffix = "jpeg"
        thumbSuffix = 'jpeg'
        thumbMIME = "image/jpeg"

        # Process the image and its thumbnail
        basewidth = CROPPED_WIDTH
        basewidth_thumb = CROPPED_THUMB_WIDTH
        img = Image.open(image_file)
        img_thumb = Image.open(image_file_thumb)
        wpercent = (basewidth / float(img.size[0]))
        wpercent_thumb = (basewidth_thumb / float(img_thumb.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        hsize_thumb = int((float(img_thumb.size[1]) * float(wpercent_thumb)))
        img = img.resize((basewidth, hsize), Image.ANTIALIAS)
        img_thumb = img_thumb.resize((basewidth_thumb, hsize_thumb), Image.ANTIALIAS)
        thumbnailString = StringIO.StringIO()
        thumbnailString_thumb = StringIO.StringIO()
        img.save(thumbnailString, format='JPEG', subsampling=0, quality=100)
        img_thumb.save(thumbnailString_thumb, format="JPEG", subsampling=1, quality=85)
        dataBlob_reg = thumbnailString.getvalue()
        dataBlob_thumbnail = thumbnailString_thumb.getvalue()

    else:
        suffix = MimeTypes().guess_extension(theMIME)[1:]
        thumbSuffix = 'png'
        thumbMIME = "image/png"

        # Process the image and its thumbnail
        basewidth = CROPPED_WIDTH
        basewidth_thumb = CROPPED_THUMB_WIDTH
        img = Image.open(image_file)
        img = removePNGAlphaLayer(img)
        img_thumb = Image.open(image_file_thumb)
        img_thumb = removePNGAlphaLayer(img_thumb)
        wpercent = (basewidth / float(img.size[0]))
        wpercent_thumb = (basewidth_thumb / float(img_thumb.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        hsize_thumb = int((float(img_thumb.size[1]) * float(wpercent_thumb)))
        img = img.resize((basewidth, hsize), Image.ANTIALIAS)
        img_thumb = img_thumb.resize((basewidth_thumb, hsize_thumb), Image.ANTIALIAS)
        thumbnailString = StringIO.StringIO()
        thumbnailString_thumb = StringIO.StringIO()
        img.save(thumbnailString, format="PNG", optimize=True)
        img_thumb.save(thumbnailString_thumb, format="PNG", quality=90, optimize=True)
        dataBlob_reg = thumbnailString.getvalue()
        dataBlob_thumbnail = thumbnailString_thumb.getvalue()

    # Write the image to a gzip blob
    out = StringIO.StringIO()
    with gzip.GzipFile(fileobj=out, compresslevel=9, mode="w") as f:
        f.write(dataBlob_reg)
    dataBlob_reg = out.getvalue()

    # Write the thumbnail to a gzip blob
    out = StringIO.StringIO()
    with gzip.GzipFile(fileobj=out, compresslevel=9, mode="w") as f:
        try:
            f.write(dataBlob_thumbnail)
        except:
            print("Failed to save thumb. Using the full size instead")
            thumbSuffix = suffix
            thumbMIME = theMIME
            f.write(dataBlob_reg)
    dataBlob_thumbnail = out.getvalue()

    # Connect to the Amazon S3 bucket
    conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME, validate=False)

    # Create the image file name
    theTimeString = unicode(int(time.time()))[5:]
    filename = unicode(identifier) + "__" + theTimeString

    # Update the return dictionary
    returnDict = {"filename": filename, "caption": fileCaption}

    # Main file upload
    k = Key(bucket)
    k.key = unicode(photo_type) + "/" + filename + "." + suffix
    k.set_contents_from_string(dataBlob_reg,
                               headers={'Content-Encoding': 'gzip', 'Content-Type': theMIME,
                                        'Cache-Control': 'max-age=86400'}, policy='public-read')
    k.make_public()
    mainPhotoURL = k.generate_url(expires_in=0, query_auth=False)

    # Thumbnail upload
    k = Key(bucket)
    k.key = unicode(photo_type) + "/" + filename + "_100x100." + thumbSuffix
    k.set_contents_from_string(dataBlob_thumbnail,
                               headers={'Content-Encoding': 'gzip', 'Content-Type': thumbMIME,
                                        'Cache-Control': 'max-age=86400'}, policy='public-read')
    k.make_public()
    thumbnailPhotoURL = k.generate_url(expires_in=0, query_auth=False)
    # print(thumbnailPhotoURL)

    # Close the connection to the Amazon S3 bucket
    conn.close()

    return {"mainPhotoURL": mainPhotoURL, "thumbnailPhotoURL": thumbnailPhotoURL, "returnDict": returnDict}
Exemple #16
0
    def effect(self):
        #get input file and copy it to some new temporary directory
        inputfile = self.options.inputfile
        if not os.path.exists(inputfile):
            inkex.utils.debug(
                "The input file does not exist. Please select a *.dxf or *.dwg file and try again."
            )
            exit(1)
        temp_input_dir = os.path.join(tempfile.gettempdir(), "dxfdwg_input")
        shutil.rmtree(temp_input_dir, ignore_errors=True
                      )  #remove the input directory before doing new job
        if not os.path.exists(temp_input_dir):
            os.mkdir(temp_input_dir)  #recreate blank dir
        shutil.copy2(
            inputfile, os.path.join(
                temp_input_dir,
                Path(inputfile).name))  # complete target filename given

        #Prepapre output conversion
        outputfilebase = os.path.splitext(os.path.basename(inputfile))[0]
        inputfile_ending = os.path.splitext(os.path.basename(inputfile))[1]
        temp_output_dir = os.path.join(tempfile.gettempdir(), "dxfdwg_output")
        shutil.rmtree(temp_output_dir, ignore_errors=True
                      )  #remove the output directory before doing new job
        if not os.path.exists(temp_output_dir):
            os.mkdir(temp_output_dir)

        #Prepare some more options for proceeding
        autocad_version = self.options.oda_outputformat.split("_")[0]
        autocad_format = self.options.oda_outputformat.split("_")[1]
        self.options.oda_audit_repair = "1" if self.options.oda_audit_repair else "0"  #overwrite string bool with int value
        entityspace = []
        if self.options.allentities or self.options.THREE_DFACE:
            entityspace.append("3DFACE")
        if self.options.allentities or self.options.ARC:
            entityspace.append("ARC")
        if self.options.allentities or self.options.BLOCK:
            entityspace.append("BLOCK")
        if self.options.allentities or self.options.CIRCLE:
            entityspace.append("CIRCLE")
        if self.options.allentities or self.options.ELLIPSE:
            entityspace.append("ELLIPSE")
        if self.options.allentities or self.options.LINE:
            entityspace.append("LINE")
        if self.options.allentities or self.options.LWPOLYLINE:
            entityspace.append("LWPOLYLINE")
        if self.options.allentities or self.options.POINT:
            entityspace.append("POINT")
        if self.options.allentities or self.options.POLYLINE:
            entityspace.append("POLYLINE")
        if self.options.allentities or self.options.POP_TRAFO:
            entityspace.append("POP_TRAFO")
        if self.options.allentities or self.options.SEQEND:
            entityspace.append("SEQEND")
        if self.options.allentities or self.options.SOLID:
            entityspace.append("SOLID")
        if self.options.allentities or self.options.SPLINE:
            entityspace.append("SPLINE")
        if self.options.allentities or self.options.TABLE:
            entityspace.append("TABLE")
        if self.options.allentities or self.options.VERTEX:
            entityspace.append("VERTEX")
        if self.options.allentities or self.options.VIEWPORT:
            entityspace.append("VIEWPORT")

        if self.options.allentities or self.options.THREE_DSOLID:
            entityspace.append("3DSOLID")
        if self.options.allentities or self.options.ATTRIB:
            entityspace.append("ATTRIB")
        if self.options.allentities or self.options.BODY:
            entityspace.append("BODY")
        if self.options.allentities or self.options.ARC_DIMENSION:
            entityspace.append("ARC_DIMENSION")
        if self.options.allentities or self.options.HATCH:
            entityspace.append("HATCH")
        if self.options.allentities or self.options.IMAGE:
            entityspace.append("IMAGE")
        if self.options.allentities or self.options.INSERT:
            entityspace.append("INSERT")
        if self.options.allentities or self.options.MESH:
            entityspace.append("MESH")
        if self.options.allentities or self.options.MTEXT:
            entityspace.append("MTEXT")
        if self.options.allentities or self.options.RAY:
            entityspace.append("RAY")
        if self.options.allentities or self.options.REGION:
            entityspace.append("REGION")
        if self.options.allentities or self.options.SHAPE:
            entityspace.append("SHAPE")
        if self.options.allentities or self.options.SURFACE:
            entityspace.append("SURFACE")
        if self.options.allentities or self.options.TRACE:
            entityspace.append("TRACE")
        if self.options.allentities or self.options.UNDERLAY:
            entityspace.append("UNDERLAY")
        if self.options.allentities or self.options.XLINE:
            entityspace.append("XLINE")

        #ODA to ezdxf mapping
        oda_ezdxf_mapping = []
        oda_ezdxf_mapping.append(
            ["ACAD9", "R12", "AC1004"]
        )  #this mapping is not supported directly. so we use the lowest possible which is R12
        oda_ezdxf_mapping.append(
            ["ACAD10", "R12", "AC1006"]
        )  #this mapping is not supported directly. so we use the lowest possible which is R12
        oda_ezdxf_mapping.append(["ACAD12", "R12", "AC1009"])
        oda_ezdxf_mapping.append(
            ["ACAD13", "R2000", "AC1012"]
        )  #R13 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
        oda_ezdxf_mapping.append(
            ["ACAD14", "R2000", "AC1014"]
        )  #R14 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
        oda_ezdxf_mapping.append(["ACAD2000", "R2000", "AC1015"])
        oda_ezdxf_mapping.append(["ACAD2004", "R2004", "AC1018"])
        oda_ezdxf_mapping.append(["ACAD2007", "R2007", "AC1021"])
        oda_ezdxf_mapping.append(["ACAD2010", "R2010", "AC1024"])
        oda_ezdxf_mapping.append(["ACAD2013", "R2013", "AC1027"])
        oda_ezdxf_mapping.append(["ACAD2018", "R2018", "AC1032"])

        ezdxf_autocad_format = None
        for oe in oda_ezdxf_mapping:
            if oe[0] == autocad_version:
                ezdxf_autocad_format = oe[1]
                break
        if ezdxf_autocad_format is None:
            inkex.errormsg("ezdxf conversion format version unknown")

        #Prepare DXF and SVG paths
        dxf_file = os.path.join(temp_output_dir, outputfilebase + ".dxf")
        svg_file = os.path.join(temp_output_dir, outputfilebase + ".svg")

        # Run ODA File Converter
        if self.options.oda_skip_dxf_to_dxf == False or inputfile_ending == ".dwg":
            # Executable test (check for proper configuration by checking mime type. Should return octet stream for a binary executable)
            if os.name == "nt" and "application/octet-stream" not in str(
                    MimeTypes().guess_type(
                        urllib.pathname2url(self.options.oda_fileconverter))):
                inkex.utils.debug(
                    "You selected to use ODA File Converter but it is not configured properly. Check for installation and path location or select 'Skip conversion from DXF to DXF'. You can download ODA Converter from 'https://www.opendesign.com/guestfiles/oda_file_converter'. You need to install it in order to use it."
                )
                exit(1)
            elif os.path.isfile(self.options.oda_fileconverter) == False:
                inkex.utils.debug(
                    "You selected to use ODA File Converter but it is not configured properly. Check for installation and path location or select 'Skip conversion from DXF to DXF'. You can download ODA Converter from 'https://www.opendesign.com/guestfiles/oda_file_converter'. You need to install it in order to use it."
                )
                exit(1)
            else:
                # Build and run ODA File Converter command
                oda_cmd = [
                    self.options.oda_fileconverter, temp_input_dir,
                    temp_output_dir, autocad_version, autocad_format, "0",
                    self.options.oda_audit_repair
                ]
                if os.name == 'nt' and self.options.oda_hidewindow:
                    info = subprocess.STARTUPINFO(
                    )  #hide the ODA File Converter window because it is annoying (does not work for Linux :-()
                    info.dwFlags = 1
                    info.wShowWindow = 0
                    proc = subprocess.Popen(oda_cmd,
                                            startupinfo=info,
                                            shell=False,
                                            stdout=PIPE,
                                            stderr=PIPE)
                else:
                    proc = subprocess.Popen(oda_cmd,
                                            shell=False,
                                            stdout=PIPE,
                                            stderr=PIPE)
                stdout, stderr = proc.communicate()
                if proc.returncode != 0 or (len(stderr) > 0 and
                                            stderr != b"Quit (core dumped)\n"):
                    inkex.utils.debug("ODAFileConverter failed: %d %s %s" %
                                      (proc.returncode, stdout, stderr))
                    if os.name != 'nt':
                        inkex.utils.debug(
                            "If the error message above contains a warning about wrong/missing Qt version please install the required version. You can get the installer from 'https://download.qt.io/archive/qt/'. Sadly you will need to create a free account to install. After installation please configure the shell script '/usr/bin/ODAFileConverter' to add a preceding line with content similar to 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/Qt5.14.2/5.14.2/gcc_64/lib/'."
                        )
                    exit(1)
            # check if ODA converted successfully. This is the case if no error file was created
            oda_errorfile = os.path.join(temp_output_dir,
                                         Path(inputfile).name + ".err")
            if os.path.exists(oda_errorfile):
                inkex.utils.debug(
                    "ODA File Converter failed to process the file. Cannot continue DXF/DWG import. The error message is:"
                )
                errormessage = open(oda_errorfile, 'r')
                errorlines = errormessage.readlines()
                for errorline in errorlines:
                    inkex.utils.debug(errorline.strip())
                errormessage.close()
                exit(1)

        # Do some movings/copies of skipped or processed DXF
        if self.options.oda_skip_dxf_to_dxf:  #if true we need to move the file to simulate "processed"
            shutil.move(os.path.join(temp_input_dir,
                                     Path(inputfile).name),
                        os.path.join(temp_output_dir,
                                     Path(inputfile).name))

        if self.options.oda_keepconverted_dxf:
            shutil.copy2(
                dxf_file,
                os.path.join(os.path.dirname(inputfile), outputfilebase +
                             "_oda.dxf"))  # complete target filename given

        # Preprocessing DXF to DXF (entity filter) by using ezdxf the first time
        if self.options.ezdxf_preprocessing:
            # uniconverter does not handle all entities. we parse the file to exlude stuff which lets uniconverter fail
            dxf = ezdxf.readfile(dxf_file)
            modelspace = dxf.modelspace()
            allowed_entities = []
            # supported entities by UniConverter- impossible: MTEXT TEXT INSERT and a lot of others
            query_string = str(entityspace)[1:-1].replace("'",
                                                          "").replace(",", "")
            if query_string != "":
                for e in modelspace.query(query_string):
                    allowed_entities.append(e)
            #inkex.utils.debug(ezdxf_autocad_format)
            #inkex.utils.debug(self.options.ezdxf_output_version)
            if self.options.ezdxf_output_version == "SAME":
                doc = ezdxf.new(ezdxf_autocad_format)
            else:
                doc = ezdxf.new(
                    self.options.ezdxf_output_version
                )  #use the string values from inx file. Required to match the values from ezdxf library. See Python reference
            msp = doc.modelspace()
            for e in allowed_entities:
                msp.add_foreign_entity(e)
            doc.saveas(dxf_file)
            if self.options.ezdfx_keep_preprocessed:
                shutil.copy2(
                    dxf_file,
                    os.path.join(
                        os.path.dirname(inputfile), outputfilebase +
                        "_ezdxf.dxf"))  # complete target filename given

        # Make SVG from DXF
        if self.options.dxf_to_svg_parser == "sk1":
            if os.name != "nt":
                inkex.utils.debug(
                    "You selected sk1 UniConvertor but you are not running on a Windows platform. On Linux uniconverter 1.1.X can be installed using the now obsolete Python 2.7, but it will not run correctly because you finally will fail at installing liblcms1-dev library on newer systems. That leads to uncompilable sk1libs package. Unfortunately sk1 UniConvertor 2.X does not support dxf format. So please use another DXF to SVG converter."
                )
                exit(1)
            sk1_command_ending = os.path.splitext(
                os.path.splitext(
                    os.path.basename(self.options.sk1_uniconverter))[1])[0]
            if sk1_command_ending != ".cmd":
                inkex.utils.debug(
                    "You selected sk1 UniConverter but it was not configured properly. Check the path to the executable."
                )
                exit(1)
            uniconverter_cmd = [
                self.options.sk1_uniconverter, dxf_file, svg_file
            ]
            #inkex.utils.debug(uniconverter_cmd)
            proc = subprocess.Popen(uniconverter_cmd,
                                    shell=False,
                                    stdout=PIPE,
                                    stderr=PIPE)
            stdout, stderr = proc.communicate()
            if proc.returncode != 0:
                inkex.errormsg("UniConverter failed: %d %s %s" %
                               (proc.returncode, stdout, stderr))
                if self.options.opendironerror:
                    self.openExplorer(temp_output_dir)

        elif self.options.dxf_to_svg_parser == "bjnortier":
            if which("node") is None:
                inkex.utils.debug(
                    "NodeJS executable not found on path. Please check your installation."
                )
                exit(1)
            else:
                bjnortier_cmd = [
                    "node",
                    os.path.join("node_modules", "dxf", "lib", "cli.js"),
                    dxf_file, svg_file
                ]
                #inkex.utils.debug(bjnortier_cmd)
                proc = subprocess.Popen(bjnortier_cmd,
                                        shell=False,
                                        stdout=PIPE,
                                        stderr=PIPE)
                stdout, stderr = proc.communicate()
                if proc.returncode != 0:
                    inkex.errormsg(
                        "node.js DXF to SVG conversion failed: %d %s %s" %
                        (proc.returncode, stdout, stderr))
                    if self.options.opendironerror:
                        self.openExplorer(temp_output_dir)

        elif self.options.dxf_to_svg_parser == "kabeja":
            wd = os.path.join(os.getcwd(), "kabeja")
            #inkex.utils.debug(wd)
            proc = subprocess.Popen(
                "java -jar launcher.jar -nogui -pipeline svg " + dxf_file +
                " " + svg_file,
                cwd=wd,
                shell=True,
                stdout=PIPE,
                stderr=PIPE)
            stdout, stderr = proc.communicate()
            if proc.returncode != 0:
                inkex.errormsg("kabeja failed: %d %s %s" %
                               (proc.returncode, stdout, stderr))
                if self.options.opendironerror:
                    self.openExplorer(temp_output_dir)

        elif self.options.dxf_to_svg_parser == "vpype_dxf":
            try:
                from inkex.command import inkscape
                import vpype
                from vpype_cli import execute
            except Exception as e:
                inkex.errormsg(
                    "Error importing vpype. Did you properly install the vpype and vpype-dxf python modules?"
                )
                exit(1)
            doc = vpype.Document()  #create new vpype document
            command = "dread  --quantization " + str(
                self.options.vpype_quantization)
            if self.options.vpype_simplify is True:
                command += " --simplify"
            if self.options.vpype_parallel is True:
                command += " --parallel"
            #command += " '" + inputfile + "'"
            command += " '" + dxf_file + "'"

            #inkex.errormsg(command)
            doc = execute(command, doc)
            if doc.length() == 0:
                inkex.errormsg(
                    'No lines left after vpype conversion. Conversion result is empty. Cannot continue'
                )
                exit(1)
            # save the vpype document to new svg file and close it afterwards
            output_fileIO = open(svg_file, "w", encoding="utf-8")
            vpype.write_svg(output_fileIO,
                            doc,
                            page_size=None,
                            center=False,
                            source_string='',
                            layer_label_format='%d',
                            show_pen_up=False,
                            color_mode='layer')
            output_fileIO.close()
            # convert vpype polylines/lines/polygons to regular paths again. We need to use "--with-gui" to respond to "WARNING: ignoring verb FileSave - GUI required for this verb."
            cli_output = inkscape(
                svg_file,
                "--with-gui",
                actions=
                "EditSelectAllInAllLayers;EditUnlinkClone;ObjectToPath;FileSave;FileQuit"
            )
            if len(cli_output) > 0:
                self.debug(
                    _("Inkscape returned the following output when trying to run the vpype object to path back-conversion:"
                      ))
                self.debug(cli_output)

        elif self.options.dxf_to_svg_parser == "ezdxf":
            try:
                doc = ezdxf.readfile(dxf_file)
                msp = doc.modelspace()
                #for e in msp: #loop through entities
                #    inkex.errormsg(e)
                #doc.header['$DIMSCALE'] = 0.2 does not apply to the plot :-(
                #inkex.utils.debug(doc.header['$DIMSCALE'])
                #inkex.utils.debug(doc.header['$MEASUREMENT'])
                auditor = doc.audit(
                )  #audit & repair DXF document before rendering
                # The auditor.errors attribute stores severe errors, which *may* raise exceptions when rendering.
                if len(auditor.errors) == 0:
                    fig = plt.figure()
                    ax = plt.axes([0., 0., 1., 1.], xticks=[], yticks=[])
                    #ax = plt.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[])
                    ax.patches = []
                    #plt.axis('off')
                    plt.margins(0, 0)
                    plt.gca().xaxis.set_major_locator(plt.NullLocator())
                    plt.gca().yaxis.set_major_locator(plt.NullLocator())
                    plt.subplots_adjust(top=1,
                                        bottom=0,
                                        right=1,
                                        left=0,
                                        hspace=0,
                                        wspace=0)
                    out = MatplotlibBackend(fig.add_axes(ax))
                    Frontend(RenderContext(doc),
                             out).draw_layout(msp, finalize=True)
                    #plt.show()
                    #fig.savefig(os.path.join(temp_output_dir, outputfilebase + ".png"), dpi=300)
                    fig.savefig(
                        svg_file
                    )  #see https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html
            except IOError:
                inkex.errormsg("Not a DXF file or a generic I/O error.")
                exit(1)
            except ezdxf.DXFStructureError:
                inkex.errormsg("Invalid or corrupted DXF file.")
                exit(1)

        elif self.options.dxf_to_svg_parser == "legacy":
            inkex.utils.debug(
                "The selected legacy DXF to SVG parser is not supported by this extension yet. Use File > Import > *.dxf. This calls the \"dxf_input.inx\" extension."
            )
            exit(1)
        else:
            inkex.utils.debug("undefined parser")
            exit(1)

        # Write the generated SVG into InkScape's canvas
        try:
            stream = open(svg_file, 'r')
        except FileNotFoundError as e:
            inkex.utils.debug(
                "There was no SVG output generated. Cannot continue")
            exit(1)
        p = etree.XMLParser(huge_tree=True)
        doc = etree.parse(stream,
                          parser=etree.XMLParser(huge_tree=True)).getroot()
        stream.close()
        doc.set(
            'id',
            self.svg.get_unique_id("dxf_dwg_import-" +
                                   self.options.dxf_to_svg_parser + "-"))
        self.document.getroot().append(doc)

        #get children of the doc and move them one group above - we don't do this for bjnortier tool because this has different structure which we don't want to disturb
        if self.options.dxf_to_svg_parser == "sk1":
            elements = []
            emptyGroup = None
            for firstGroup in doc.getchildren():
                emptyGroup = firstGroup
                for element in firstGroup.getchildren():
                    elements.append(element)
                #break #only one cycle - could be bad idea or not
            for element in elements:
                doc.set('id', self.svg.get_unique_id('dxf_dwg_import'))
                doc.insert(doc.index(firstGroup), element)

            if emptyGroup is not None:
                emptyGroup.getparent().remove(emptyGroup)

        #empty the following vals because they destroy the size aspects of the import / make viewbox looking wrong
        if self.options.dxf_to_svg_parser == "bjnortier" or self.options.dxf_to_svg_parser == "kabeja":
            doc.set('width', '')
            doc.set('height', '')
            doc.set('viewBox', '')
            doc.getchildren()[0].set('transform', '')

        #adjust viewport and width/height to have the import at the center of the canvas
        if self.options.resizetoimport:
            bbox = inkex.elements._selected.ElementList.bounding_box(
                doc.getchildren()[0])
            if bbox is not None:
                root = self.svg.getElement('//svg:svg')
                offset = self.svg.unittouu(
                    str(self.options.extraborder) +
                    self.options.extraborder_units)
                root.set(
                    'viewBox', '%f %f %f %f' %
                    (bbox.left - offset, bbox.top - offset,
                     bbox.width + 2 * offset, bbox.height + 2 * offset))
                root.set('width', bbox.width + 2 * offset)
                root.set('height', bbox.height + 2 * offset)
Exemple #17
0
    def do_chunked_upload(self, path):
        # Prepare media file
        size = os.stat(path).st_size
        file_hash = self.hash_file(path)

        encoded_size = size * (4 / 3)

        root = self.api.get_base_folder()['id']

        media = file_parts.UDSFile(ntpath.basename(path),
                                   None,
                                   MimeTypes().guess_type(
                                       urllib.request.pathname2url(path))[0],
                                   formatter(size),
                                   formatter(encoded_size),
                                   parents=[root],
                                   size_numeric=size,
                                   md5=file_hash)

        parent = self.api.create_media_folder(media)

        # Should be the same
        no_docs = math.ceil(encoded_size / MAX_DOC_LENGTH)

        # Append all chunks to chunk list
        chunk_list = list()
        for i in range(no_docs):
            chunk_list.append(
                file_parts.Chunk(path,
                                 i,
                                 size,
                                 media=media,
                                 parent=parent['id']))

        total = 0
        total_chunks = len(chunk_list)
        progress_bar_chunks = tqdm(total=total_chunks,
                                   unit='chunks',
                                   dynamic_ncols=True,
                                   position=0)
        progress_bar_speed = tqdm(total=total_chunks * CHUNK_READ_LENGTH_BYTES,
                                  unit_scale=1,
                                  unit='B',
                                  dynamic_ncols=True,
                                  position=1)

        for chunk in chunk_list:
            total += 1
            self.upload_chunked_part(chunk)
            progress_bar_speed.update(CHUNK_READ_LENGTH_BYTES)
            progress_bar_chunks.update(1)
        #
        # Concurrently execute chunk upload and report back when done.
        # with concurrent.futures.ProcessPoolExecutor(max_workers=MAX_WORKERS_ALLOWED) as executor:
        #     for file in executor.map(ext_upload_chunked_part, chunk_list):
        #         total = total + file
        #         elapsed_time = round(time.time() - start_time, 2)
        #         current_speed = round(
        #             (total) / (elapsed_time * 1024 * 1024), 2)
        #         progress_bar("Uploading %s at %sMB/s" %
        #                   (media.name, current_speed), total, size)
        # ""
        # Print new file output
        table = [[media.name, media.size, media.encoded_size, parent['id']]]
        print(" \r")
        print("\n" +
              tabulate(table, headers=[
                  'Name',
                  'Size',
                  'Encoded',
                  'ID',
              ]))
Exemple #18
0
 def setanalyzedFileType(self, path):
     mime = MimeTypes()
     url = urllib.pathname2url(path)
     mime_type = mime.guess_type(url)
     self.fileType = mime_type
Exemple #19
0
 def do_GET(self):
  print("************************************************")  
  mime=MimeTypes()
  mimeType=mime.guess_type(self.path)
  print("Request aayi")
  urlParseData=urlparse(self.path) 
  print("URLPARSEDATA****  ",urlParseData)
  urlPath=urlParseData[2]
  query=urlParseData[4]
  print("QUERY *****",query)
  if query!='':
   query=parse_qs(query)
   print(query)
   print("name ",query["nm"][0])
  newPath=''
  if urlPath=='/' or urlPath=='/favicon.ico':
   newPath='./default/index.html'
  # print("default wale if me agya")
  else:
   path=urlPath
   path=path.split("/")
   print(path[1])
   if path[1] in siteNames :
    if len(path)==2:
     print('./apps/'+path[1]+'/index.html')
     if os.path.isfile('./apps/'+path[1]+'/index.html'):	
      print('index.html found')
     # urlPath='./apps/'+path[1]+'/index.html'
      newPath=urlPath+'/index.html'
     else:
      print('index.html not found')
      if os.path.isfile('./apps/'+path[1]+'/index.htm'):
       print('index.htm found')
       #urlPath='./apps/'+path[1]+'/index.htm'
       newPath=urlPath+'/index.htm'
      else:
       print('index.htm not found')
       if os.path.isfile('./apps/'+path[1]+'/index.py'):
        print("index.py found")
       # urlPath='./apps/'+path[1]+'/index.py'
        newPath=urlPath+'/index.py'
       else:
        self.send_error(404,"Homepage not found")
        return
     self.send_response(301)
     self.send_header('Location',newPath) 
     self.end_headers()
     return
    else:
     if path[2]=='private':
#      print("Kuch nahi ho sakta")
      self.wfile.write(bytes("404 Resource Not Found","utf-8"))
     else:
      a=self.path.find("/")
      print("index ",a)
      pathHavingResourceDetail=urlPath[urlPath.find("/",a+1):]
      print("aaaaabcdcdbvava",pathHavingResourceDetail)
      mapping=siteNames[path[1]].getMapping()
      if pathHavingResourceDetail in mapping:
        print("py file exists.")
        a=mapping[pathHavingResourceDetail].rfind(".")
        if a!=-1:
         fileName=mapping[pathHavingResourceDetail][a+1:]
         print(fileName)
         packageName=mapping[pathHavingResourceDetail][:a]
         print(packageName)
         finalpath='apps.'+path[1]+".private."+packageName+"."+fileName
         moduleName=__import__(finalpath,fromlist=fileName)
         #request=Request
         self.send_response(200)
         request=Request(self,query)
         response=Response(self,self.wfile,'text/html')
         moduleName.process(request,response)
         #self.send_header('Content-type',response.getContentType())
         #self.end_headers()
         return
        else:
         finalpath='apps.'+path[1]+".private."+mapping[pathHavingResourceDetail]
         moduleName=__import__(finalpath,fromlist=mapping[pathHavingResourceDetail])
         self.send_response(200)
         request=Request(self,query)
         response=Response(self,self.wfile,'text/html')
         moduleName.process(request,response)
         #self.send_header('Content-type',response.getContentType())
         #self.end_headers()
         return 
      else:
       newPath='./apps'+urlPath#self.path[self.path.find("/"):]
       mimeType=mime.guess_type(newPath)	
       
   else:
    self.send_error(404,'%s Not Found' %(path[1]))
  try:
   #mimeType=mime.guess_type(newPath)
   print(mimeType)
   print(newPath) 
   f=open(newPath,'rb')
   self.send_response(200)
   self.send_header('Content-type',mimeType)
   self.end_headers()
   self.wfile.write(f.read())
   f.close()
  except IOError:
   print("File does not exist");
   self.send_error(404,'File Not Found: %s' % urlPath)
 # print(urlPath)
  return
Exemple #20
0
 def guess_mime_type(path: Path) -> Optional[str]:
     # guess_type expects a str as argument because
     # it is intended to be used with urls and not with paths
     mime_type = MimeTypes().guess_type(str(path))
     return mime_type[0]
 def get_mimetype(path):
     return MimeTypes().guess_type(path)[0] if path is not None else ""
Exemple #22
0
class Scaffold:
    APP_VERSION = f"Pyrogram {__version__}"
    DEVICE_MODEL = f"{platform.python_implementation()} {platform.python_version()}"
    SYSTEM_VERSION = f"{platform.system()} {platform.release()}"

    LANG_CODE = "en"

    PARENT_DIR = Path(sys.argv[0]).parent

    INVITE_LINK_RE = re.compile(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:joinchat/|\+))([\w-]+)$")
    WORKERS = min(32, os.cpu_count() + 4)
    WORKDIR = PARENT_DIR
    CONFIG_FILE = PARENT_DIR / "config.ini"

    PARSE_MODES = ["combined", "markdown", "md", "html", None]

    mimetypes = MimeTypes()
    mimetypes.readfp(StringIO(mime_types))

    def __init__(self):
        try:
            asyncio.get_event_loop()
        except RuntimeError:
            # This happens when creating Client instances inside different threads that don't have an event loop.
            # Set the main event loop in this thread.
            asyncio.set_event_loop(pyrogram.main_event_loop)

        self.session_name = None
        self.api_id = None
        self.api_hash = None
        self.app_version = None
        self.device_model = None
        self.system_version = None
        self.lang_code = None
        self.ipv6 = None
        self.proxy = None
        self.test_mode = None
        self.bot_token = None
        self.phone_number = None
        self.phone_code = None
        self.password = None
        self.force_sms = None
        self.workers = None
        self.workdir = None
        self.config_file = None
        self.plugins = None
        self.parse_mode = None
        self.no_updates = None
        self.takeout = None
        self.sleep_threshold = None

        self.executor = None

        self.storage = None

        self.rnd_id = MsgId

        self.parser = Parser(self)
        self.parse_mode = "combined"

        self.session = None

        self.media_sessions = {}
        self.media_sessions_lock = asyncio.Lock()

        self.is_connected = None
        self.is_initialized = None

        self.no_updates = None
        self.takeout_id = None

        self.dispatcher = None

        self.disconnect_handler = None

        self.loop = None

    async def send(self, *args, **kwargs):
        pass

    async def resolve_peer(self, *args, **kwargs):
        pass

    def fetch_peers(self, *args, **kwargs):
        pass

    def add_handler(self, *args, **kwargs):
        pass

    async def save_file(self, *args, **kwargs):
        pass

    async def get_messages(self, *args, **kwargs):
        pass

    async def get_history(self, *args, **kwargs):
        pass

    async def get_dialogs(self, *args, **kwargs):
        pass

    async def get_chat_members(self, *args, **kwargs):
        pass

    async def get_chat_members_count(self, *args, **kwargs):
        pass

    async def answer_inline_query(self, *args, **kwargs):
        pass

    async def get_profile_photos(self, *args, **kwargs):
        pass

    async def edit_message_text(self, *args, **kwargs):
        pass

    async def edit_inline_text(self, *args, **kwargs):
        pass

    async def edit_message_media(self, *args, **kwargs):
        pass

    async def edit_inline_media(self, *args, **kwargs):
        pass

    async def edit_message_reply_markup(self, *args, **kwargs):
        pass

    async def edit_inline_reply_markup(self, *args, **kwargs):
        pass

    def guess_mime_type(self, *args, **kwargs):
        pass

    def guess_extension(self, *args, **kwargs):
        pass

    def load_config(self, *args, **kwargs):
        pass

    def load_session(self, *args, **kwargs):
        pass

    def load_plugins(self, *args, **kwargs):
        pass

    async def handle_download(self, *args, **kwargs):
        pass

    async def start(self, *args, **kwargs):
        pass

    async def stop(self, *args, **kwargs):
        pass

    async def connect(self, *args, **kwargs):
        pass

    async def authorize(self, *args, **kwargs):
        pass

    async def disconnect(self, *args, **kwargs):
        pass

    async def initialize(self, *args, **kwargs):
        pass

    async def terminate(self, *args, **kwargs):
        pass
Exemple #23
0
from mimetypes import MimeTypes
from os import path

from crawlmi.http import Response, TextResponse, XmlResponse, HtmlResponse
from crawlmi.utils.python import is_binary

_mime_types = MimeTypes()
_mt_filename = path.join(path.dirname(path.abspath(__file__)), 'mime.types')
with open(_mt_filename, 'rb') as f:
    _mime_types.readfp(f)

_response_classes = {
    'text/html': HtmlResponse,
    'application/atom+xml': XmlResponse,
    'application/rdf+xml': XmlResponse,
    'application/rss+xml': XmlResponse,
    'application/xhtml+xml': HtmlResponse,
    'application/vnd.wap.xhtml+xml': HtmlResponse,
    'application/xml': XmlResponse,
    'application/json': TextResponse,
    'application/javascript': TextResponse,
    'application/x-javascript': TextResponse,
    'text/xml': XmlResponse,
    'text/*': TextResponse,
}


def from_mime_type(mime_type):
    '''Return the most appropiate Response class for the given mime type.'''
    if mime_type is None:
        return Response
Exemple #24
0
def send_campaign_email_to_admin_contact(campaign,
                                         domain='demo.django-crm.io',
                                         protocol='https'):
    try:
        campaign = Campaign.objects.get(id=campaign)
        attachments = []
        if campaign.attachment:
            file_path = campaign.attachment.path
            file_name = file_path.split("/")[-1]
            content = open(file_path, 'rb').read()
            mime = MimeTypes()
            mime_type = mime.guess_type(file_path)
            attachments.append((file_name, content, mime_type[0]))
        subject = campaign.subject
        contacts = ContactEmailCampaign.objects.all()
        default_html = campaign.html_processed
        blocked_domains = BlockedDomain.objects.values_list('domain',
                                                            flat=True)
        blocked_emails = BlockedEmail.objects.values_list('email', flat=True)
        for each_contact in contacts:
            if ((each_contact.email not in blocked_emails) and
                (each_contact.email.split('@')[-1] not in blocked_domains)):
                html = default_html
                if campaign.reply_to_email:
                    reply_to_email = campaign.reply_to_email
                else:
                    domain_name = 'django-crm.com'
                    if campaign.from_email is not None:
                        from_email = campaign.from_email
                    else:
                        from_email = campaign.created_by.email
                    reply_to_email = str(from_email) + ' <' + \
                        str(settings.EMAIL_HOST_USER + '@' + domain_name + '') + '>'

                # domain_url = settings.URL_FOR_LINKS
                domain_url = protocol + '://' + domain
                # img_src_url = domain_url + reverse('marketing:campaign_open', kwargs={
                #     'campaign_log_id': campaign_log.id, 'email_id': each_contact.id})
                # # images can only be accessed over https
                # link = '<img src={img_src_url} alt="company_logo" title="company_logo" height="1" width="1" />'.format(
                #     img_src_url=img_src_url)
                # link = '<img src="' + domain_url + '/m/cm/track-email/' + \
                #     str(campaign_log.id) + '/contact/' + \
                #     str(each_contact.id) + '/" height="1" width="1" alt="company_logo" + \
                #     title="company_logo"/>'

                # unsubscribe_from_campaign_url = reverse(
                #     'marketing:unsubscribe_from_campaign', kwargs={'contact_id': each_contact.id,
                #                                                     'campaign_id': campaign.id})
                # unsubscribe_from_campaign_html = "<br><br/><a href={}>Unsubscribe</a>".format(
                #     domain_url + unsubscribe_from_campaign_url)

                # names_dict = {'company_name': '', 'city': '', 'state': '',
                #                 'last_name': each_contact.last_name if each_contact.last_name else '',
                #                 'email': each_contact.email, 'email_id': each_contact.id,
                #                 'name': each_contact.name + ' ' + each_contact.last_name if each_contact.last_name else '',
                #             }

                # mail_html = html + link + unsubscribe_from_campaign_html
                html = Template(html).render(
                    Context({'email_id': each_contact.id}))
                mail_html = html
                from_email = str(campaign.from_name) + "<" + \
                    str(campaign.from_email) + '>'
                to_email = [each_contact.email]
                send_campaign_mail(subject, mail_html, from_email, to_email,
                                   [], [reply_to_email], attachments)
    except Exception as e:
        print(e)
        pass
Exemple #25
0
def sign():
    ###################################
    # request JSON structure:
    # {
    #     user_id: codice_fiscale // "X"*15 to skip check
    #     file_list: [
    #         {
    #             file: file_path,
    #             signed_file_type: p7m|pdf
    #         },
    # TODO sistema struttura
    #         {
    #             file: file_path,
    #             signed_file_type: p7m|pdf
    #         },
    #         ...
    #     ],
    #     output_path: output_folder_path
    # }
    ###################################
    MyLogger().my_logger().info("/api/sign request")

    # check for well formed request JSON
    invalid_json_request = "Richiesta al server non valida, contatta l'amministratore di sistema"

    if not request.json:
        error_message = "Missing json request structure"
        return error_response_maker(error_message, invalid_json_request, 404)

    if not "user_id" in request.json:
        error_message = "missing user_id field"
        return error_response_maker(error_message, invalid_json_request, 404)
    user_id = request.json["user_id"]

    if not "file_list" in request.json:
        error_message = "missing file_list field"
        return error_response_maker(error_message, invalid_json_request, 404)
    file_list = request.json["file_list"]

    if not isinstance(file_list, (list, )) or len(file_list) < 1:
        error_message = "Empty file_list"
        return error_response_maker(error_message, invalid_json_request, 404)

    for json_file in file_list:
        if not "file" in json_file:
            error_message = "missing file field"
            return error_response_maker(error_message, invalid_json_request,
                                        404)

        if not "signed_file_type" in json_file:
            error_message = "missing signed_file_type field"
            return error_response_maker(error_message, invalid_json_request,
                                        404)
        sig_type = json_file["signed_file_type"]

        if not allowed_signature(sig_type):
            error_message = f"{sig_type} not allowed in signed_file_type field"
            return error_response_maker(error_message, invalid_json_request,
                                        404)

        if "sig_attributes" in json_file:
            sig_attributes = json_file["sig_attributes"]
        else:
            MyLogger().my_logger().error(
                f"missing sig_attributes field for file {json_file['file']}")
            continue

# TODO check altri campi

    if not "output_path" in request.json:
        error_message = "missing output_path field"
        return error_response_maker(error_message, invalid_json_request, 404)
    path_for_signed_files = request.json["output_path"]

    output_to_url = False
    if path_for_signed_files.startswith("http://"):
        output_to_url = True
    else:
        if not path.exists(path_for_signed_files) or not path.isdir(
                path_for_signed_files):
            error_message = f"{path_for_signed_files} field is not a valid directory"
            return error_response_maker(error_message, invalid_json_request,
                                        404)

    # folder cleanup
    for _file in listdir(SIGNED_FOLDER):
        remove(path.join(SIGNED_FOLDER, _file))

    # getting smart cards connected
    try:
        sessions = DigiSignLib().get_smart_cards_sessions()
    except Exception as err:
        _, value, tb = sys.exc_info()
        MyLogger().my_logger().error(value)
        MyLogger().my_logger().error('\n\t'.join(f"{i}"
                                                 for i in extract_tb(tb)))
        clear_pin(user_id)
        return error_response_maker(
            str(err),
            "Controllare che la smart card sia inserita correttamente", 500)

    # attempt to login
    try:
        get_pin(user_id)
        session = DigiSignLib().session_login(sessions,
                                              memorized_pin[user_id]["pin"])
    except Exception as err:
        _, value, tb = sys.exc_info()
        MyLogger().my_logger().error(value)
        MyLogger().my_logger().error('\n\t'.join(f"{i}"
                                                 for i in extract_tb(tb)))
        clear_pin(user_id)
        return error_response_maker(
            str(err), "Controllare che il pin sia valido e corretto", 500)

    # loop on given files
    signed_files_list = []
    for index, file_to_sign in enumerate(file_list):
        # already checked
        signature_type = file_to_sign["signed_file_type"]
        file_path_to_sign = file_to_sign["file"]

        # initialize response structure
        output_item = {
            "file_to_sign": file_path_to_sign,
            "signed": "",
            "signed_file": ""
        }
        signed_files_list.append(output_item)

        # handle url file paths
        if file_path_to_sign.startswith("http://"):
            try:
                local_file_path = downoad_file(file_path_to_sign)
            except:
                MyLogger().my_logger().error(
                    f"Impossibile reperire il file: {file_path_to_sign}")
                _, value, tb = sys.exc_info()
                MyLogger().my_logger().error(value)
                MyLogger().my_logger().error('\n\t'.join(
                    f"{i}" for i in extract_tb(tb)))
                signed_files_list[index]["signed"] = "no"
                continue
        else:
            local_file_path = file_path_to_sign

        try:
            if signature_type == P7M:
                # p7m signature
                temp_file_path = DigiSignLib().sign_p7m(
                    local_file_path, session, user_id, sig_attributes)
            elif signature_type == PDF:
                # pdf signature
                mime = MimeTypes().guess_type(local_file_path)[0]
                if mime == 'application/pdf':
                    temp_file_path = DigiSignLib().sign_pdf(
                        local_file_path, session, user_id, sig_attributes)
                else:
                    MyLogger().my_logger().info(
                        f"the file {local_file_path} is not a pdf will be ignored"
                    )
                    signed_files_list[index]["signed"] = "no"
                    continue

            signed_files_list[index]["signed"] = "yes"
        except CertificateOwnerException as err:
            user_tip = "Codice fiscale dell'utente non corrispondente a quello della smart card. Impossibile procedere."
            DigiSignLib().session_logout(session)
            DigiSignLib().session_close(session)
            return error_response_maker(str(err), user_tip, 500)
        except:
            _, value, tb = sys.exc_info()
            MyLogger().my_logger().error(value)
            MyLogger().my_logger().error('\n\t'.join(f"{i}"
                                                     for i in extract_tb(tb)))
            signed_files_list[index]["signed"] = "no"
            continue

        # moving signed file to given destination
        if output_to_url:
            with open(temp_file_path, "rb") as _file:
                files = {'file': _file}
                try:
                    MyLogger().my_logger().info(path_for_signed_files)
                    res = post(path_for_signed_files, files=files)
                except:
                    _, value, tb = sys.exc_info()
                    MyLogger().my_logger().error(value)
                    MyLogger().my_logger().error('\n\t'.join(
                        f"{i}" for i in extract_tb(tb)))
                    signed_files_list[index]["signed_file"] = "EXCEPTION!!"
                    continue
                if res.status_code != 200:
                    error_message = res.json()["error_message"]
                    MyLogger().my_logger().error(error_message)
                    signed_files_list[index]["signed_file"] = "ERROR!!"
                    continue
                else:
                    signed_files_list[index]["signed"] = "yes - [remote]"
                    uploaded_path = res.json()["Ok"]
                    signed_files_list[index][
                        "signed_file"] = f"{uploaded_path}"
                    continue
        else:
            temp_file_name = path.basename(temp_file_path)
            signed_file_path = path.join(path_for_signed_files, temp_file_name)
            try:
                move(temp_file_path, signed_file_path)
                signed_files_list[index]["signed_file"] = signed_file_path
            except:
                _, value, tb = sys.exc_info()
                MyLogger().my_logger().error(value)
                MyLogger().my_logger().error('\n\t'.join(
                    f"{i}" for i in extract_tb(tb)))
                signed_files_list[index]["signed_file"] = "LOST"
                continue

    # Folder cleanup
    for _file in listdir(UPLOAD_FOLDER):
        remove(path.join(UPLOAD_FOLDER, _file))

    # logout
    try:
        DigiSignLib().session_logout(session)
    except:
        MyLogger().my_logger().error("logout failed")
    # session close
    try:
        DigiSignLib().session_close(session)
    except:
        MyLogger().my_logger().error("session close failed")
    ###################################
    # response JSON structure:
    # { signed_file_list: [
    #     {
    #         file_to_sign: ***,
    #         signed: yes|no,
    #         signed_file: ***
    #     },
    #     {
    #         file_to_sign: ***,
    #         signed: yes|no,
    #         signed_file: ***
    #     },
    #     ...
    # ]}
    ###################################
    res = make_response(jsonify({"signed_file_list": signed_files_list}))
    return res
Exemple #26
0
def addItemToS3FromURL_local_function(srcURL, destinationFolderPath, destinationFileNamePrefix, itemType):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'utf-8,ISO-8859-1;q=0.7,*;q=0.3',
       'Accept-Language': 'en-US,en;q=0.5',
       'Accept-Encoding': 'gzip, deflate',
       'Connection': 'keep-alive'}

    # Start a connection to Amazon
    conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME, validate=False)
    k = Key(bucket)

    # If the file is a URL / Citation thumbnail
    if itemType in ("MetaImageThumbnail"):
        try:
            # Try fetching the image
            try:
                response = requests.get(srcURL, headers=headers, allow_redirects=True, timeout=7, verify=False)
            except:
                print("******************")
                print("ERROR (addItemToS3FromURL): " + str(sys.exc_info()[0]))
                print("ATTEMPTED URL WAS: " + srcURL)
                print("******************")
                return None

            # Fill an image buffer with the fetched image data
            img = StringIO.StringIO(response.content)
            theMIME = magic.from_buffer(response.content, mime=True)

            # Check if the image is an SVG file
            if theMIME == "image/svg+xml":
                outfile = StringIO.StringIO()
                img = outfile

            # Create a PIL object from the image
            im = Image.open(img)

            # Remove the transparent / alpha layer, if present
            im = removePNGAlphaLayer(im)

            # Thumbnail down to a 250px x 250px png file
            im.thumbnail((250, 250), Image.ANTIALIAS)

            # Create an empty buffer
            out_im = cStringIO.StringIO()

            try:
                # Try to guess the extension from the MIME type
                theExtension = MimeTypes().guess_extension(theMIME)[1:]

                # Save the image file in memory
                im.save(out_im, theExtension)
            except Exception, e:
                # If MIME was undetermined, try to convert to a PNG
                print('No extension determined, trying PNG')
                try:
                    im.convert('RGB').save(out_im, 'PNG', quality=90, optimize=True)
                    print("Failed to save as optimized PNG, will try something else")
                except Exception, e:
                    print str(e)
                    try:
                        im.convert('RGB').save(out_im, 'PNG', quality=90)
                        print("Failed to save as unoptimized PNG, will try something else")
                    except Exception, e:
                        print('Unable to thumbnail')
Exemple #27
0
    def run(self, path):
        if path == "/":
            f = open(
                os.path.join(config.get_runtime_path(), "platformcode",
                             "template", "page.html"), "rb")
            self.handler.send_response(200)
            self.handler.send_header('Content-type', 'text/html')
            self.handler.end_headers()
            respuesta = f.read()
            respuesta = respuesta.replace("{$WSPORT}",
                                          config.get_setting("websocket.port"))
            self.handler.wfile.write(respuesta)
            f.close()

        elif path.startswith("/local/"):
            import base64
            import urllib
            Path = path.replace("/local/", "").split("/")[0]
            Path = base64.b64decode(urllib.unquote_plus(Path))
            Size = int(os.path.getsize(Path.decode("utf8")))
            f = open(Path.decode("utf8"), "rb")
            if not self.handler.headers.get("range") == None:
                if "=" in str(
                        self.handler.headers.get("range")) and "-" in str(
                            self.handler.headers.get("range")):
                    Inicio = int(
                        self.handler.headers.get("range").split("=")[1].split(
                            "-")[0])
                    if self.handler.headers.get("range").split("=")[1].split(
                            "-")[1] <> "":
                        Fin = int(
                            self.handler.headers.get("range").split("=")
                            [1].split("-")[1])
                    else:
                        Fin = Size - 1

            else:
                Inicio = 0
                Fin = Size - 1

            if not Fin > Inicio: Fin = Size - 1

            if self.handler.headers.get("range") == None:
                logger.info(
                    "-------------------------------------------------------")
                logger.info("Solicitando archivo local: " + Path)
                logger.info(
                    "-------------------------------------------------------")

                self.handler.send_response(200)
                self.handler.send_header("Content-Disposition",
                                         "attachment; filename=video.mp4")
                self.handler.send_header('Accept-Ranges', 'bytes')
                self.handler.send_header('Content-Length', str(Size))
                self.handler.send_header("Connection", "close")
                self.handler.end_headers()
                while True:
                    time.sleep(0.2)
                    buffer = f.read(1024 * 250)
                    if not buffer:
                        break
                    self.handler.wfile.write(buffer)
                self.handler.wfile.close()
                f.close()
            else:
                logger.info(
                    "-------------------------------------------------------")
                logger.info("Solicitando archivo local: " + Path)
                logger.info("Rango: " + str(Inicio) + "-" + str(Fin) + "/" +
                            str(Size))
                logger.info(
                    "-------------------------------------------------------")
                f.seek(Inicio)

                self.handler.send_response(206)
                self.handler.send_header("Content-Disposition",
                                         "attachment; filename=video.mp4")
                self.handler.send_header('Accept-Ranges', 'bytes')
                self.handler.send_header('Content-Length', str(Fin - Inicio))
                self.handler.send_header(
                    'Content-Range',
                    str(Inicio) + "-" + str(Fin) + "/" + str(Size))
                self.handler.send_header("Connection", "close")

                self.handler.end_headers()
                while True:
                    time.sleep(0.2)
                    buffer = f.read(1024 * 250)
                    if not buffer:
                        break
                    self.handler.wfile.write(buffer)
                self.handler.wfile.close()
                f.close()
        elif path.startswith("/media/"):
            file = os.path.join(config.get_runtime_path(), "platformcode",
                                "template", path[7:])
            from mimetypes import MimeTypes
            mime = MimeTypes()
            mime_type = mime.guess_type(file)
            try:
                mim = mime_type[0]
            except:
                mim = ""
            f = open(file, "rb")
            self.handler.send_response(200)
            self.handler.send_header('Content-type', mim)
            self.handler.end_headers()
            self.handler.wfile.write(f.read())
            f.close()
Exemple #28
0
def addMediaImage(request, pageID, theFile, fileComment, PLACEHOLDER_LIST, inputMime):
    # Connect to the Amazon S3 bucket
    conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME, validate=False)
    k = Key(bucket)

    # Check for placeholder descriptions and reject them
    placeholderPresent = any(placeholder in fileComment for placeholder in PLACEHOLDER_LIST)
    if (placeholderPresent):
        return HttpResponse("ERROR_NO_DESCRIPTION")
    else:
        pass

    # Detect the MIME type
    if inputMime != "EMPTY":
        theMIME = inputMime
    else:
        theMIME = magic.from_buffer(theFile.read(), mime=True)

    # Check for bad MIME types
    if str(theMIME) in ["application/x-empty", "application/octet-stream"]:
        mime_type = MimeTypes().guess_type(theFile.name)
        theMIME = mime_type[0]

    # Seek to the beginning of the file buffer
    theFile.seek(0)

    # Create the file name and suffix
    unique_id = str(uuid4())
    mimeSuffixWithDot = MimeTypes().guess_extension(theMIME)
    theSlug = DjangoText.slugify(unicode(strip_tags(fileComment)).encode('utf-8'), allow_unicode=True)[:50]
    theNameSlugged = DjangoText.slugify(unicode(pageID).encode('utf-8'), allow_unicode=True)
    k.key = u"NewlinkFiles/" + unicode(pageID) + u"/" + unique_id[:5] + "___" +  theNameSlugged + u"/" + theSlug + mimeSuffixWithDot

    # Upload the file to the S3 bucket
    if "video" in theMIME:
        # Don't gzip videos due to streaming errors
        k.set_contents_from_string(theFile.read(), headers={'Content-Type': theMIME, 'Cache-Control': 'max-age=86400'})

    else:
        out = StringIO.StringIO()
        with gzip.GzipFile(fileobj=out, compresslevel=9, mode="w") as f:
            f.write(theFile.read())
        dataBlobGzip = out.getvalue()

        k.set_contents_from_string(dataBlobGzip, headers={'Content-Encoding': 'gzip', 'Content-Type': theMIME, 'Cache-Control': 'max-age=86400'})

    # Generate a URL
    k.make_public()
    fileEndPath = k.key
    mainMediaURL = k.generate_url(expires_in=0, query_auth=False)

    # Get the thumbnail
    thumb100url = None
    if "video" in theMIME:
        thumb100url = "https://epcdn-vz.azureedge.net/static/images/placeholder-video.png"
    elif "image" in theMIME:
        try:
            pictureRootPath = os.path.splitext(fileEndPath)[0]

            # Open connection to Amazon S3
            conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
            bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME, validate=False)

            # Thumbnail upload
            image_file_thumb = theFile

            # Seek to the beginning to the buffer
            theFile.seek(0)

            # Check if the file is an SVG image
            if theMIME == "image/svg+xml":
                outfile = StringIO.StringIO()
                image_file_thumb = outfile

            # Format and generate the thumbnail
            basewidth_thumb = CROPPED_META_THUMB_WIDTH
            img_thumb = Image.open(image_file_thumb)
            img_thumb = removePNGAlphaLayer(img_thumb)
            wpercent_thumb = (basewidth_thumb / float(img_thumb.size[0]))
            hsize_thumb = int((float(img_thumb.size[1]) * float(wpercent_thumb)))
            img_thumb = img_thumb.resize((basewidth_thumb, hsize_thumb), Image.ANTIALIAS)
            thumbnailString_thumb = StringIO.StringIO()
            thumbMIME = theMIME
            theExtension = MimeTypes().guess_extension(theMIME)[1:]
            thumbExtension = theExtension

            # Try to save the thumbnail to memory
            try:
                img_thumb.save(thumbnailString_thumb, theExtension, )
            except Exception, e:
                print str(e)
                try:
                    img_thumb.convert('RGB').save(thumbnailString_thumb, 'PNG', quality=90, optimize=True)
                    thumbExtension = "png"
                    thumbMIME = "image/png"
                except Exception, e:
                    print("Failed to save as optimized PNG, will try something else")
                    print str(e)
                    try:
                        img_thumb.convert('RGB').save(thumbnailString_thumb, 'PNG', quality=90)
                        thumbExtension = "png"
                        thumbMIME = "image/png"
                    except Exception, e:
                        print("Failed to save as unoptimized PNG, will try something else")
                        print str(e)
Exemple #29
0
def run_campaign(campaign, domain='demo.django-crm.io', protocol='https'):
    try:
        campaign = Campaign.objects.get(id=campaign)
        attachments = []
        if campaign.attachment:
            file_path = campaign.attachment.path
            file_name = file_path.split("/")[-1]
            content = open(file_path, 'rb').read()
            mime = MimeTypes()
            mime_type = mime.guess_type(file_path)
            attachments.append((file_name, content, mime_type[0]))
        subject = campaign.subject

        contacts = Contact.objects.filter(contact_list__in=[
            each_list for each_list in campaign.contact_lists.all()
        ])
        default_html = campaign.html_processed
        for each_contact in contacts:
            html = default_html
            campaign_log = CampaignLog.objects.create(contact=each_contact,
                                                      campaign=campaign)
            if campaign.reply_to_email:
                reply_to_email = campaign.reply_to_email
            else:
                message_id = get_campaign_message_id(campaign_log)
                campaign_log.message_id = message_id
                campaign_log.save()
                domain_name = 'django-crm.com'
                if campaign.from_email is not None:
                    from_email = campaign.from_email
                else:
                    from_email = campaign.created_by.email
                reply_to_email = str(from_email) + ' <' + \
                    str(message_id + '@' + domain_name + '') + '>'
            if not (each_contact.is_bounced or each_contact.is_unsubscribed):
                # domain_url = settings.URL_FOR_LINKS
                domain_url = protocol + '://' + domain
                img_src_url = domain_url + reverse(
                    'marketing:campaign_open',
                    kwargs={
                        'campaign_log_id': campaign_log.id,
                        'email_id': each_contact.id
                    })
                # images can only be accessed over https
                link = '<img src={img_src_url} alt="company_logo" title="company_logo" height="1" width="1" />'.format(
                    img_src_url=img_src_url)
                # link = '<img src="' + domain_url + '/m/cm/track-email/' + \
                #     str(campaign_log.id) + '/contact/' + \
                #     str(each_contact.id) + '/" height="1" width="1" alt="company_logo" + \
                #     title="company_logo"/>'

                unsubscribe_from_campaign_url = reverse(
                    'marketing:unsubscribe_from_campaign',
                    kwargs={
                        'contact_id': each_contact.id,
                    })
                unsubscribe_from_campaign_html = "<br><br/><a href={}>Unsubscribe</a>".format(
                    domain_url + unsubscribe_from_campaign_url)
                names_dict = {
                    'company_name':
                    each_contact.company_name
                    if each_contact.company_name else '',
                    'last_name':
                    each_contact.last_name if each_contact.last_name else '',
                    'city':
                    each_contact.city if each_contact.city else '',
                    'state':
                    each_contact.state if each_contact.state else '',
                    'first_name':
                    each_contact.name,
                    'email':
                    each_contact.email,
                    'email_id':
                    each_contact.id,
                    'unsubscribe_from_campaign_url':
                    unsubscribe_from_campaign_url
                }

                html = Template(html).render(Context(names_dict))
                mail_html = html + link + unsubscribe_from_campaign_html
                from_email = str(campaign.from_name) + "<" + \
                    str(campaign.from_email) + '>'
                to_email = [each_contact.email]
                send_campaign_mail(subject, mail_html, from_email, to_email,
                                   [], [reply_to_email], attachments)
    except Exception as e:
        print(e)
        pass
Exemple #30
0
def run_campaign(campaign, domain="demo.django-crm.io", protocol="https"):
    blocked_domains = BlockedDomain.objects.values_list("domain", flat=True)
    blocked_emails = BlockedEmail.objects.values_list("email", flat=True)
    try:
        campaign = Campaign.objects.get(id=campaign)
        attachments = []
        if campaign.attachment:
            file_path = campaign.attachment.path
            file_name = file_path.split("/")[-1]
            content = open(file_path, "rb").read()
            mime = MimeTypes()
            mime_type = mime.guess_type(file_path)
            attachments.append((file_name, content, mime_type[0]))
        subject = campaign.subject

        contacts = Contact.objects.filter(contact_list__in=[
            each_list for each_list in campaign.contact_lists.all()
        ])
        default_html = campaign.html_processed
        for each_contact in contacts:
            html = default_html
            campaign_log = CampaignLog.objects.create(contact=each_contact,
                                                      campaign=campaign)
            if campaign.reply_to_email:
                reply_to_email = campaign.reply_to_email
            else:
                message_id = get_campaign_message_id(campaign_log)
                campaign_log.message_id = message_id
                campaign_log.save()
                domain_name = "django-crm.com"
                if campaign.from_email is not None:
                    from_email = campaign.from_email
                else:
                    from_email = campaign.created_by.email
                reply_to_email = (str(from_email) + " <" +
                                  str(message_id + "@" + domain_name + "") +
                                  ">")
            if not (each_contact.is_bounced or each_contact.is_unsubscribed):
                if (each_contact.email not in blocked_emails) and (
                        each_contact.email.split("@")[-1]
                        not in blocked_domains):
                    # domain_url = settings.URL_FOR_LINKS
                    domain_url = protocol + "://" + domain
                    img_src_url = domain_url + reverse(
                        "marketing:campaign_open",
                        kwargs={
                            "campaign_log_id": campaign_log.id,
                            "email_id": each_contact.id,
                        },
                    )
                    # images can only be accessed over https
                    link = '<img src={img_src_url} alt="company_logo" title="company_logo" height="1" width="1" />'.format(
                        img_src_url=img_src_url)
                    # link = '<img src="' + domain_url + '/m/cm/track-email/' + \
                    #     str(campaign_log.id) + '/contact/' + \
                    #     str(each_contact.id) + '/" height="1" width="1" alt="company_logo" + \
                    #     title="company_logo"/>'

                    unsubscribe_from_campaign_url = reverse(
                        "marketing:unsubscribe_from_campaign",
                        kwargs={
                            "contact_id": each_contact.id,
                            "campaign_id": campaign.id,
                        },
                    )
                    unsubscribe_from_campaign_html = "<br><br/><a href={}>Unsubscribe</a>".format(
                        domain_url + unsubscribe_from_campaign_url)
                    names_dict = {
                        "company_name":
                        each_contact.company_name
                        if each_contact.company_name else "",
                        "last_name":
                        each_contact.last_name
                        if each_contact.last_name else "",
                        "city":
                        each_contact.city if each_contact.city else "",
                        "state":
                        each_contact.state if each_contact.state else "",
                        "first_name":
                        each_contact.name,
                        "email":
                        each_contact.email,
                        "email_id":
                        each_contact.id,
                        "name":
                        each_contact.name + " " + each_contact.last_name
                        if each_contact.last_name else "",
                        "unsubscribe_from_campaign_url":
                        unsubscribe_from_campaign_url,
                    }

                    html = Template(html).render(Context(names_dict))
                    mail_html = html + link + unsubscribe_from_campaign_html
                    from_email = (str(campaign.from_name) + "<" +
                                  str(campaign.from_email) + ">")
                    to_email = [each_contact.email]
                    send_campaign_mail(
                        subject,
                        mail_html,
                        from_email,
                        to_email,
                        [],
                        [reply_to_email],
                        attachments,
                    )
    except Exception as e:
        print(e)
        pass